This post is completed by 2 users

  • 1
Add to List
Beginner

111. Find Unique Number in Array with Duplicates (Except One)

Objec­tive: - Given an Integer array. An array contains duplicates of all the numbers in the array except for one number. Find that number.

Example :

int [] A = { 2,1,3,5,5,3,2,1,6,7,7,8,8};
Output: Missing duplicate is 6

Approach:

  • The naive solution is to use map or dictionary (Space complexity - O(n))
  • Better solution - XOR
  • A^A = 0 and A^B^A = B, so if we XOR all the elements, the answer will be the missing number.
  • If we have only one element, the missing no will be that no

 

Output:

Missing duplicate is 6