-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?

8
  • What is bla, boi here? Commented Jan 15, 2018 at 13:54
  • Please try searching before asking Commented Jan 15, 2018 at 13:55
  • 1
    @Pointy This is not duplicate of that question. Expected output is different Commented Jan 15, 2018 at 13:57
  • @Bharadwaj, now with right dupe. Commented Jan 15, 2018 at 14:03
  • @Bharadwaj I didn't vote to close it as a duplicate; I voted to close it as "too broad" or "where's the code". Commented Jan 15, 2018 at 14:03

1 Answer 1

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);

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

1 Comment

@Bharadwaj, I have updated my answer. Thanks for correcting me.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.