• Advertisement
Sign in to follow this  

Binary search.. or something

This topic is 4467 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have an array of numbers, say: 1,2,3,7,7,7,8,9,9. From this array I want to make another array containing only the unique numbers (no duplicates), like so: 1,2,3,7,8,9. Simple, do something like this: for each number in array1 if not number exists in array2 then add number to array2 The problem is that I want it to be fairly fast and because of the array is huge looping through array2 to check if a number already exists gets slow. So I was thinking of sorting the arrays and do a binary search. Or is there some other clever way?

Share this post


Link to post
Share on other sites
Advertisement
Assuming C++, sort the array and then use std::unique_copy() to copy to the new array.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement