We have N numbers(real numbers(float)).They can be positive or negative or anything we do not know except the fact that there is exactly one number which has a duplicate and the rest all are distinct. I would like to know the fastest one to find the duplicates.Can anyone help ? Thanks
|
closed as too localized by GlenH7, Glenn Nelson, Martijn Pieters, gnat, ElYusubov Feb 20 at 21:46
This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, see the FAQ.
If we were dealing with perfect equality (hairy when dealing with floating point numbers) you could simply use the numbers as keys of a hash table & use that to count occurances, looking for one where you have a duplicate. Since we've defined 'equality' as allowing for some fuziness, the simplest solution would be to sort the array (or a copy) and loop through it until you find an adjacent pair that are "equal". |
|||
|
Simplest would probably be to iterate across the inputs and build a sorted list. You can use a binary search on the list, which will either find the duplicate or at least point you to the place where your new number should be inserted. Keep adding the numbers until you find the duplicate. This should be more efficient than sorting the entire input set and searching it (since you should only have to process half the elements, on average), although if you use the system sort routine the code will certainly be simpler. |
|||
|
I would say the simplest algorithm would be to sort the array. Then iterate the array from |
|||
|