Find the only unique element in an array of a million elements

前端 未结 4 603
余生分开走
余生分开走 2021-01-04 09:46

I was asked this question in a recent interview.

You are given an array that has a million elements. All the elements are duplicates except one. My task is to find t

相关标签:
4条回答
  • 2021-01-04 10:20

    In fact, since the number of elements in the array was fix, you could do much better than what you have proposed.

    By "creating a map with index as the number in the array and the value as the frequency of the number occurring in the array", you create a map with 2^32 positions (assuming the array had 32-bit integers), and then you have to pass though that map to find the first position whose value is one. It means that you are using a large auxiliary space and in the worst case you are doing about 10^6+2^32 operations (one million to create the map and 2^32 to find the element).

    Instead of doing so, you could sort the array with some n*log(n) algorithm and then search for the element in the sorted array, because in your case, n = 10^6.

    For instance, using the merge sort, you would use a much smaller auxiliary space (just an array of 10^6 integers) and would do about (10^6)*log(10^6)+10^6 operations to sort and then find the element, which is approximately 21*10^6 (many many times smaller than 10^6+2^32).

    PS: sorting the array decreases the search from a quadratic to a linear cost, because with a sorted array we just have to access the adjacent positions to check if a current position is unique or not.

    0 讨论(0)
  • 2021-01-04 10:21

    Your approach seems fine. It could be that he was looking for an edge-case where the array is of even size, meaning there is either no unmatched elements or there are two or more. He just went about asking it the wrong way.

    0 讨论(0)
  • 2021-01-04 10:30

    I'm certain that you can't solve this problem without going through the whole array, at least if you don't have any additional information (like the elements being sorted and restricted to certain values), so the problem has a minimum time complexity of O(n). You can, however, reduce the memory complexity to O(1) with a XOR-based solution, if every element is in the array an even number of times, which seems to be the most common variant of the problem, if that's of any interest to you:

    int unique(int[] array)
    {
        int unpaired = array[0];
        for(int i = 1; i < array.length; i++)
            unpaired = unpaired ^ array[i];
        return unpaired;
    }
    

    Basically, every XORed element cancels out with the other one, so your result is the only element that didn't cancel out.

    0 讨论(0)
  • 2021-01-04 10:40

    Assuming the array is un-ordered, you can't. Every value is mutually exclusive to the next so nothing can be deduced about a value from any of the other values?

    If it's an ordered array of values, then that's another matter and depends entirely on the ordering used.

    I agree the easiest way is to have another container and store the frequency of the values.

    0 讨论(0)
提交回复
热议问题