Alex Rivera | Logout

Algorithm to find a number which occurs only once in an array, given all the other numbers occur twice

Asked 2009-07-07T01:43:12.903
19

What I can think of is:

Algo:

  1. Have a hash table which will store the number and its associated count
  2. Parse the array and increment the count for number.
  3. Now parse the hash table to get the number whose count is 1.

Can you guys think of solution better than this. With O(n) runtime and using no extra space

Edit
Report

1 Answer

0

Algo 2:

  1. Sort the array.
  2. Now parse the array and if 2 consecutive numbers are not same we got our number.
  3. This will not use extra space
answered 2009-07-07T01:50:02.443

Your Answer