Sword Finger offer-38. Repeated Numbers in Array (39)

Posted by jrobles on Mon, 07 Oct 2019 08:39:54 +0200

38. Repeated numbers in arrays (39)

  • Description: All numbers in an array of length n are in the range of 0 to n-1.
    Some numbers in an array are duplicated, but it is not known how many are duplicated. I don't know how many times each number is repeated. Find any duplicate number in the array.
    For example, if an array {2,3,1,0,2,5,3} with an input length of 7 is input, the corresponding output is the first duplicate number 2.

  • Code

    package _38.Repeated Numbers in Arrays;
    
    import java.util.Arrays;
    
    public class Duplicate {
    	
    	public static void main(String[] args) {
    		int numbers[] = {2,3,1,0,2,5,3};
    		int duplication[] = new int[1];
    		duplicate3(numbers,7,duplication);
    		System.out.println(duplication[0]);
    	}
    	/**
    	 * Solution 1: Violent solution; use two loops to traverse; do not change the order of the original array. Time Complexity: O(n^2) Space Complexity: O(1)
    	 * 
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate1(int numbers[], int length, int[] duplication) {
    		if (numbers == null || length < 2)
    			return false;
    		for (int i = 0; i < numbers.length; i++) {
    			for (int j = i + 1; j < numbers.length; j++) {
    				if (numbers[i] == numbers[j]) {
    					duplication[0] = numbers[i];
    					return true;
    				}
    			}
    		}
    		return false;
    	}
    
    	/**
    	 * Solution 2: Sort arrays and find repetitive elements.
    	 *  	  The time complexity is O(nlogn)
    	 *       The spatial complexity is O(1)
    	 * 
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate2(int numbers[], int length, int[] duplication) {
    		if (numbers == null || length < 2)
    			return false;
    		// Arrays.sort(numbers);
    		quickSort(numbers, 0, length - 1);
    		System.out.println(Arrays.toString(numbers));
    		for (int i = 0; i < length; i++) {
    			if(numbers[i] == numbers[i+1]){
    				duplication[0] = numbers[i];
    				return true;
    			}
    		}
    		return false;
    	}
    
    	// Quick row
    	public static void quickSort(int arr[], int start, int end) {
    		// Find the location of the first element in the current array
    		if (start < end) {
    			// Use the first element in the array as a benchmark to locate it
    			int stard = arr[start];
    			// Traverse the array from the front to the back, respectively, when low==high
    			int low = start;
    			int high = end;
    			while (low < high) {
    				while (arr[high] >= stard && low < high) {
    					high--;
    				}
    				arr[low] = arr[high];
    				while (arr[low] < stard && low < high) {
    					low++;
    				}
    				arr[high] = arr[low];
    			}
    			arr[high] = stard;
    
    			// Processing elements on the left
    			quickSort(arr, start, high - 1);
    			// Processing the elements on the right
    			quickSort(arr, high + 1, end);
    		}
    	}
    	
    	/**
    	 * Solution 3: Because the size of the elements in the array is between [0,n-1],
    	 *        You can use another auxiliary space to save whether the elements in the array are duplicated or not.
    	 *        The size of the element in the array corresponds to the state in which the auxiliary space is subordinated to that element.
    	 * @param numbers
    	 * @param length
    	 * @param duplication
    	 * @return
    	 */
    	public static boolean duplicate3(int numbers[], int length, int[] duplication) {
    		if(numbers == null || length < 2) return false;
    		
    		boolean state[] = new boolean[length];
    		for(int i = 0; i < length; i++){
    			if(state[numbers[i]] == true){//Revisit
    				duplication[0] = numbers[i];
    				return true;
    			}
    			state[numbers[i]] = true;//Indicates that number[i] has been visited
    		}
    		return false;
    	}
    }
    

Topics: Java