1

To simplify my question, suppose the objects are jqlite objects, using the angular.equals function, I can check whether they are equal. My question is: How do we use this function to remove duplicate item from an array of jqLite objects?

Here is what I tried:

// Suppose jqArr is the array stated above:
var result = [];

angular.forEach(jqArr, function(v_i){
   if(result.length === 0){
      result.push(v_i);
   } else {
      var isPushed = false;
      angular.forEach(result, function(v_j){
         if(angualr.equals(v_i, v_j)){
           isPushed = true;
         }
      });
      if(isPushed === false){
         result.push(v_i);
      }
   }
})
console.log(result);

Suppose jqArr = [e_1, e_2, e_3, e_1, e_2], where e_i(s) are jQLite elements. output should be:

[e_1, e_2, e_3]

*Please answer using only javascript and angularJs.

Tel lui
  • 139
  • 1
  • 2
  • 13
  • Possible duplicate of [Remove Duplicates from JavaScript Array](http://stackoverflow.com/questions/9229645/remove-duplicates-from-javascript-array) – JEY Mar 24 '17 at 10:10

2 Answers2

1

You can use ES6 Set like so:

let arr = [1,1,2,2,2,3,4,5,6,6,6,6,6];
let uniq = [...new Set(arr)];

The uniq array will contain unique values. If the aray is filled with object references, it will naturally work too.

Greg
  • 1,851
  • 2
  • 19
  • 21
  • I run this in my chrome, by letting arr = [1,1,{a:1},2,2,3,4,{a:1},6,6,6,6,6]; let uniq = [...new Set(arr)]; but I get this result: [1, Object, 2, 3, 4, Object, 6]. Is this the problem of chrome version? – Tel lui Mar 24 '17 at 10:42
  • You are creating new objects by inserting object literals inside an array directly. Those are two different objects there. If you declared an object beforehand and insert a reference to the object in the array twice, than the method I provided you would work. Remeber this: `{a:1} !== {a:1}`. But `let o = {a:1}; o === o;` – Greg Mar 24 '17 at 20:58
0

In a more abstract form, you are performing an O(n^2) algorithm (same with indexOf and Set) but you can reduce the complexity to O(nlogn) by adding all the elements to the list without checks, after all the elements have been collected remove the duplicates be sorting (needs only one pass over the array to remove duplicates after sort).

This solution works only if you can store all the duplicates, for a "duplication factor" above 100% this is not efficient.

If you cannot perform a logic sort, hash function can give the same result.

MaanooAk
  • 2,418
  • 17
  • 28