-4
var arr = [1, 1, 1, 5, 3, 4, 6, 6]

function uniqueRetriever(bla, boi) {
...
}

var unique = uniqueRetriever(bla, boi)

console.log(unique);

//output: [5, 3, 4]

How do I retrieve unique elements from an array without changing the original array?

Pointy
  • 405,095
  • 59
  • 585
  • 614

1 Answers1

1

Try filter() like the following:

var arr = [1, 1, 1, 5, 3, 4, 6, 6];

var res = arr.filter(function (item, index, arr) {
    var count = 0;
    for(var i = 0; i < arr.length; ++i){
        if(arr[i] == item)
            count++;
    }
    // check if item appears once then return item
    if(count == 1){
      return arr.indexOf(item) === index;
    }
})

console.log(res);
Mamun
  • 66,969
  • 9
  • 47
  • 59