All:
Suppose I have tons of points (x, y), could anyone show me a fast way(simple algorithm preferred) to remove the duplicated position value points in Javascript?
What I can think about is like: sort them by x and compare one by one.
All:
Suppose I have tons of points (x, y), could anyone show me a fast way(simple algorithm preferred) to remove the duplicated position value points in Javascript?
What I can think about is like: sort them by x and compare one by one.
What you seem to be looking for is some sort of HashSet
for Javascript. Conveniently enough for our purposes, Javascript Objects
behave enough like that to do what you want in a really simple way:
// Assuming points is an array of objects that look like {x:i, y:i}
var uniquePoints = {};
for (var i = 0; len = points.length; i < len; i++) {
var point = points[i];
uniquePoint[point.x + '_' + point.y] = point;
}
At the end of the for
loop, you'll have an object containing all unique points.