-1

For Example my input Array = [1,2,3,2,3,5] I want this array as output, where I want to remove all the numbers that repeat. output Array = [1,5] How do I remove all multiple values?

  • Does this answer your question? [Get all unique values in a JavaScript array (remove duplicates)](https://stackoverflow.com/questions/1960473/get-all-unique-values-in-a-javascript-array-remove-duplicates) – jonrsharpe Aug 29 '21 at 15:08
  • I want uniqueness as if any number repeats I want to remove both duplicate numbers, For Example, Array = [2,2,3,4,5,5] Output should be outPutArray = [3,5] – Muneeb Nawaz Aug 29 '21 at 15:23

2 Answers2

0

I would combine the following two answers:

Get all unique values in a JavaScript array (remove duplicates)

How to find index of all occurrences of element in array?

function getAllIndexes(arr, val) {
    var indexes = [], i = -1;
    while ((i = arr.indexOf(val, i+1)) != -1){
        indexes.push(i);
    }
    return indexes;
}

function onlyUnique(value, index, self) {
  return getAllIndexes(self, value).length == 1
}

var inputArray = [1,2,3,2,3,5]
inputArray = inputArray.filter(onlyUnique)
console.log(inputArray)
Nopileos
  • 1,976
  • 7
  • 17
  • Can you please explain the above code, as I am a beginner and I cannot understand the above code, Thanks in advance – Muneeb Nawaz Sep 06 '21 at 13:26
0

You can use set and spread operator I believe

uniq = [...new Set(array)];
Maddy
  • 2,025
  • 5
  • 26
  • 59