Let’s say I have an array:
double[][] points = {{0.0, 0.0}, {1.0, 1.0}, {1.0, 1.0}, {2.0, 2.0}};
I want to create a new array without the duplicate entry {1.0, 1.0} – what would be the best way to do so?
Additional info:
-
The array is sorted, but only by the first component, so it’s possible to have
{1.0, 2.0}, {1.0, 1.0}, {1.0, 2.0}That’s how I get the data, I cannot change the initial sort mechanism.
-
Two dimensions are the current limit, but the array can have thousands of points.
Simplest answer: Compare the elements of the array pair-wise and remove the duplicates. This won’t scale well, but it might not need to.
More complicated: Look at something like a radix sort. After you’ve sorted by first and then second elements of the subarrays, you can walk through the whole array and remove duplicates. This will scale better, but it could easily be overkill (depending on your situation).
Best (probably): Create a set of array elements. Walk through the array; for each element, check to see if it is already in the set. If it is, remove it from the array. If not, add it to the set and keep going. This is probably the best approach unless duplicating the array is a space issue.