Find duplicate element in array in time O(n)

前端 未结 24 2671
余生分开走
余生分开走 2020-11-27 10:07

I have been asked this question in a job interview and I have been wondering about the right answer.

You have an array of numbers from 0 to n-1, one o

相关标签:
24条回答
  • 2020-11-27 11:00
    public class FindDuplicate {
        public static void main(String[] args) {
            // assume the array is sorted, otherwise first we have to sort it.
            // time efficiency is o(n)
            int elementData[] = new int[] { 1, 2, 3, 3, 4, 5, 6, 8, 8 };
            int count = 1;
            int element1;
            int element2;
    
            for (int i = 0; i < elementData.length - 1; i++) {
                element1 = elementData[i];
                element2 = elementData[count];
                count++;
                if (element1 == element2) {
                    System.out.println(element2);
                }
            }
        }
    }
    
    0 讨论(0)
  • 2020-11-27 11:01

    As described,

    You have an array of numbers from 0 to n-1, one of the numbers is removed, and replaced with a number already in the array which makes a duplicate of that number.

    I'm assuming elements in the array are sorted except the duplicate entry. If this is the scenario , we can achieve the goal easily as below :

            public static void main(String[] args) {
        //int arr[] = { 0, 1, 2, 2, 3 };
        int arr[] = { 1, 2, 3, 4, 3, 6 };
        int len = arr.length;
        int iMax = arr[0];
        for (int i = 1; i < len; i++) {
            iMax = Math.max(iMax, arr[i]);
            if (arr[i] < iMax) {
                System.out.println(arr[i]);
                break;
            }else if(arr[i+1] <= iMax) {
                System.out.println(arr[i+1]);
                break;
            }
        }
    }
    
    • O(n) time and O(1) space ;please share your thoughts.
    0 讨论(0)
  • 2020-11-27 11:02
    1. sort the array O(n ln n)
    2. using the sliding window trick to traverse the array O(n)

      Space is O(1)

      Arrays.sort(input);
      for(int i = 0, j = 1; j < input.length ; j++, i++){
          if( input[i] == input[j]){
              System.out.println(input[i]);
              while(j < input.length && input[i] == input[j]) j++;
              i = j - 1;
          }
      }
      

    Test case int[] { 1, 2, 3, 7, 7, 8, 3, 5, 7, 1, 2, 7 }

    output 1, 2, 3, 7

    0 讨论(0)
  • 2020-11-27 11:05

    One working solution:

    asume number are integers

    create an array of [0 .. N]

    int[] counter = new int[N];
    

    Then iterate read and increment the counter:

     if (counter[val] >0) {
       // duplicate
     } else {
       counter[val]++;
     }
    
    0 讨论(0)
  • 2020-11-27 11:06

    Use a HashSet to hold all numbers already seen. It operates in (amortized) O(1) time, so the total is O(N).

    0 讨论(0)
  • 2020-11-27 11:07

    Scan the array 3 times:

    1. XOR together all the array elements -> A. XOR together all the numbers from 0 to N-1 -> B. Now A XOR B = X XOR D, where X is the removed element, and D is the duplicate element.
    2. Choose any non-zero bit in A XOR B. XOR together all the array elements where this bit is set -> A1. XOR together all the numbers from 0 to N-1 where this bit is set -> B1. Now either A1 XOR B1 = X or A1 XOR B1 = D.
    3. Scan the array once more and try to find A1 XOR B1. If it is found, this is the duplicate element. If not, the duplicate element is A XOR B XOR A1 XOR B1.
    0 讨论(0)
提交回复
热议问题