Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find the one non-repeating element in array?

I have an array of n elements in which only one element is not repeated, else all the other numbers are repeated >1 times. And there is no limit on the range of the numbers in the array.

Some solutions are:

  • Making use of hash, but that would result in linear time complexity but very poor space complexity
  • Sorting the list using MergeSort O(nlogn) and then finding the element which doesn't repeat

Is there a better solution?

like image 576
Luv Avatar asked Apr 20 '12 04:04

Luv


People also ask

How do you find non-repeating elements in an array using XOR?

First, calculate the XOR of all the array elements. All the bits that are set in xor will be set in one non-repeating element (x or y) and not in others. So if we take any set bit of xor and divide the elements of the array in two sets – one set of elements with same bit set and another set with same bit not set.

How do you find repeating elements in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How do you find unique elements 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 the third non-repeating element in an array?

To check the status of visited elements create a array of size n. Run a loop from index 0 to n and check if (visited[i]==1) then skip that element. Otherwise create a variable count = 1 to keep the count of frequency. Check if(arr[i]==arr[j]), then increment the count by 1 and set visited[j]=1.


1 Answers

One general approach is to implement a bucketing technique (of which hashing is such a technique) to distribute the elements into different "buckets" using their identity (say index) and then find the bucket with the smallest size (1 in your case). This problem, I believe, is also known as the minority element problem. There will be as many buckets as there are unique elements in your set.

Doing this by hashing is problematic because of collisions and how your algorithm might handle that. Certain associative array approaches such as tries and extendable hashing don't seem to apply as they are better suited to strings.

One application of the above is to the Union-Find data structure. Your sets will be the buckets and you'll need to call MakeSet() and Find() for each element in your array for a cost of $O(\alpha(n))$ per call, where $\alpha(n)$ is the extremely slow-growing inverse Ackermann function. You can think of it as being effectively a constant.

You'll have to do Union when an element already exist. With some changes to keep track of the set with minimum cardinality, this solution should work. The time complexity of this solution is $O(n\alpha(n))$.

Your problem also appears to be loosely related to the Element Uniqueness problem.

like image 95
rrufai Avatar answered Sep 20 '22 00:09

rrufai