0

I try to draw a graph from tens of thousands points, but because this number is so big i need to reduce it. Many of them have duplicates. I tried to reduce the number using this:

   var array=_.reject(data,function(object,i){
return i>0 && (data[i-1].a === object.a && data[i-1].b===object.b && data[i-1].c===object.c);
});

How can i modify this function, or to create a new one, in order to keep first and last value considered duplicate. Those are different by another attribute 'd' which represent a time stamp.

3
  • It will help if you can share some sample input and output. Commented Oct 11, 2017 at 6:33
  • stackoverflow.com/questions/1960473/unique-values-in-an-array Commented Oct 11, 2017 at 6:37
  • Thanks. I think this it is similar with my function. but still does not keep at least two points. Commented Oct 11, 2017 at 6:41

3 Answers 3

2
//return filtered points, compareFunction for sorting, equalFunction for
//removing points
function removeDuplicate(data,compareFunction,equalFunction) {
  data.sort(function(pointa, pointb) {
    var compare = compareFunction(pointa,pointb);
    return compare;
  });
  var arr = new Array();
  var prev = new Object();
  var index = 0;
  for (var i = 0; i < data.length; i++) {
    if (i == 0 || !(equalFunction(prev,data[i]))) {
      arr[index++] = data[i];
      prev = data[i];
    }
  }
return arr;
}

function compareFunction(pointa,pointb){
return (pointa.a + pointa.b + pointa.c) - (pointb.a + pointb.b + pointb.c);
}

function equalFunction(pointa,pointb){
return pointa.a == pointb.a && pointa.b == pointb.b && pointa.c == pointb.c;
}

example - https://jsfiddle.net/8xu4Lwp2/

Sign up to request clarification or add additional context in comments.

Comments

1

The simplest way to eliminate duplicates from an array in JavaScript is to cast it as a Set and then back to an Array. Sets don't store duplicates.

// not sure why setArr isn't logging, it does in Opera console.

arr=[1,1,2,2,3,3];
console.log(arr);
setArr=new Set(arr);
console.log(setArr);
newArr=[...setArr];
console.log(newArr);

Comments

-1

Cool solution:

var unique = Array.from(new Set(arrayWithDuplicatedValue));

3 Comments

Doesn't work with object array Array.from(new Set([{a:1}, {a:1}]));
@gurvinder372 That's not an error to vote -1, objects has different references, if you try {a:1} === {a:1} the result is false. But if you try var foo = {a:1}; Array.from(new Set([foo, foo])) then works properly.
Your solution, therefore, is irrelevant to the question at best. OP is comparing objects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.