Skip to main content
gamedev.net
🔒 Locked

Binary search.. or something

Started by AndersO Nov 28, 2005 at 6:04 PM 1 replies 650+ views
Original Post
AndersO
AndersO
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?
SiCrane
SiCrane
Assuming C++, sort the array and then use std::unique_copy() to copy to the new array.
AndersO
AndersO
Cool, thanks!.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.