(Not logged on) | Register | Log On

You can subscribe to this discussion group using an RSS feed reader. techInterview Discussion

Answers to technical interview questions. A part of TechInterview.org

A part of techInterview.org: answers to technical interview questions.

Your host: Michael Pryor

Unique number in a array of integers.

Hello,

I recently encountered this question in an interview and answered it properly. As I have gained so much from this forum , I just wanted to share this question, which might help other aspirants.

There is an array with 2n+1  numbers and all the numbers are present two times except one , find that unique number ?
Rahul.b Send private email
Friday, June 11, 2010
 
 
I remember that XOR is handy for this.
jdk Send private email
Friday, June 11, 2010
 
 
integer xoring=0
for i=1 to 2n+1
  xoring=xoring xor thearray[i]
print xoring    // This is the one-occurrence value.

Sincerely,

Gene Wirchenko
Gene Wirchenko Send private email
Friday, June 11, 2010
 
 
This is a common interview trick.
Yaxiong Zhao Send private email
Friday, June 11, 2010
 
 
One variant of this question is, where,
the numbers are sorted and you have to find the unique entry in log(n) time.[as opposed to O(n) time for XORing]
puneet Send private email
Friday, June 11, 2010
 
 
It relies on the property that there should be same number on the odd position *and* even position; after passing the unique number, the same number is on even and odd position.
Yaxiong Zhao
Saturday, June 12, 2010
 
 

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
 
Powered by FogBugz