Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

arrays

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 the unique element.

var arr = [3, 4, 3, 2, 2, 6, 7, 2, 3........]

My approach was to go through the entire array in a for loop, and then create a map with index as the number in the array and the value as the frequency of the number occurring in the array. Then loop through our map again and return the index that has value of 1.

I said my approach would take O(n) time complexity. The interviewer told me to optimize it in less than O(n) complexity. I said that we cannot, as we have to go through the entire array with a million elements.

Finally, he didn't seem satisfied and moved onto the next question.

I understand going through million elements in the array is expensive, but how could we find a unique element without doing a linear scan of the entire array?

PS: the array is not sorted.

like image 516
Wild Widow Avatar asked Jun 03 '16 22:06

Wild Widow


People also ask

How do you find only unique elements in an array?

Step 1 − Declare an array and input the array elements at run time. Step 2 − Start traversing the array and check, if the current element is already present in an array or not. Step 3 − If it is already present in an array then, move to the next element in an array and continue.

How do you find unique numbers in an array?

We can use bitwise AND to find the unique element in O(n) time and constant extra space. Create an array count[] of size equal to number of bits in binary representations of numbers. Fill count array such that count[i] stores count of array elements with i-th bit set. Form result using count array.

How do you find unique elements in an array using XOR?

The way to find the unique element is by using the XOR bitwise operator which returns 1 only if one of the element is 1 and false otherwise. In the loop, each of the integers in the array are XORed with uniqueId starting at 0. Then, 0 is XOR'ed with 34.

What is a unique value in an array?

You can find the distinct values in an array using the Distinct function. The Distinct function takes the array as an input parameter and returns another array that consists only of the unique, or non-duplicate, elements. The following example shows how to find the distinct values in an array.


2 Answers

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.

like image 101
Noctiphobia Avatar answered Oct 19 '22 21:10

Noctiphobia


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.

like image 27
lfgtm Avatar answered Oct 19 '22 21:10

lfgtm