How to delete dupicates in array, but not just one element that have pair to make him unique, but both, or how much duplicates there is?

How to delete dupicates in array, but not just one element that have pair to make him unique, but both, or how much duplicates there is?
0

#1

have: [ 1, 2, 3, 1, 2, 4, 5, 6, 2]
output: [ 3, 4, 5, 6]

I need this for: https://www.freecodecamp.com/challenges/symmetric-difference


#2

Are you familar with the indexOf method of an array? You could use that to remove duplicates. You can loop through the array and then do a indexof of the item you are currently on. If you take your example of [ 1, 2, 3, 1, 2, 4, 5, 6, 2] the indexOf(1) is 0 since it is the first item in the array. As you loop through the array and you get to the second 1 which is in position 3 the indexOf(1) is 0 and 0 does not match the position 3 so that means it is a duplicate and you remove it.

Hope that helps.


#3

I think the easiest way to do this is with a map (or hash, or dictionary, depending on what your language calls it).

You’d have a map where the key is each integer, and the value is the count of instances of that integer in the array.

Example in Python:

#!/usr/bin/env python

from collections import defaultdict

input =  [1, 2, 3, 1, 2, 4, 5, 6, 2]

vals = defaultdict(lambda: 0)

for val in input:
    vals[val] += 1

output = [key for key, val in vals.items() if val == 1]

print output

Output:

[3, 4, 5, 6]

Example in Go:

package main

import (
    "fmt"
)

func main() {
    input := []int{1, 2, 3, 1, 2, 4, 5, 6, 2}
    vals := make(map[int]int)
    for _, val := range input {
        vals[val]++
    }
    output := make([]int, 0)
    for key, val := range vals {
        if val == 1 {
            output = append(output, key)
        }
    }
    fmt.Println(output)
}

Output:

[3 4 5 6]

#4

In JS, can something like this:

function filterer(array){
  
   var have = {};
   return array.filter(function(val) {
     return have.hasOwnProperty(val) ? false : (have[val] = true);
      });


}