This week's book giveaway is in the Agile and other Processes forum.
We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line!
See this thread for details.
The moose likes Java in General and the fly likes Best algorithm to find the duplicate number Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Best algorithm to find the duplicate number" Watch "Best algorithm to find the duplicate number" New topic
Author

Best algorithm to find the duplicate number

Sriram Sharma
Ranch Hand

Joined: Apr 12, 2006
Posts: 89
Hi All,

This is an interview question faced by my friend and hence posting here looking for some best answers.

There are 100 numbers out of which one number has a duplicate while all other numbers are unique.
What is the best algorithm to find the duplicate number.
Having a for loop/iterating is really a school kids work.
Need some good way out.

I coud think of only quick sort algorithm, as in the process of sorting we can find the duplicate also (checking for the equal value).
Any good suggestions please...!!!

Regards,
Sriram
Gupta Tarun
Greenhorn

Joined: Sep 16, 2010
Posts: 22

Hi Sriram,

I think when we talk about efficiency we need to consider Space-time tradeoff.

So I think in this case if time is your criteria and not the space, then sorting the entire array is not a good idea, alternative can be using HashSet. add() and check the returned boolean value, if its false the value that you are adding already exists in the set hence its a duplicate number, Time wise it be more efficient then sorting the entire array but consume more memory space.

Regards
Tarun
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Best algorithm to find the duplicate number
 
Similar Threads
Java Questions in Interview
Practical Number?
URLyBird DBMain's create delete update method?
Decrpytion Using SHA-1
combination generator