1// usage example:
2var myArray = ['a', 1, 'a', 2, '1'];
3var unique = myArray.filter((v, i, a) => a.indexOf(v) === i);
4
5// unique is ['a', 1, 2, '1']
1//ES6
2let uniqueArray = [...new Set(arrayWithDuplicates)];
3
4//Alternative
5function removeArrayDuplicates(arrayWithDuplicates) {
6 let seen = {};
7 let uniqueArray = [];
8 let len = arrayWithDuplicates.length;
9 let j = 0;
10 for(let i = 0; i < len; i++) {
11 let item = arrayWithDuplicates[i];
12 if(seen[item] !== 1) {
13 seen[item] = 1;
14 uniqueArray[j++] = item;
15 }
16 }
17 return uniqueArray;
18}
1function getNotUnique(array) {
2 var map = new Map();
3 array.forEach(a => map.set(a, (map.get(a) || 0) + 1));
4 return array.filter(a => map.get(a) > 1);
5}
6
7console.log(getNotUnique([1, 2, 2, 4, 4])); //[2, 2, 4, 4]
8console.log(getNotUnique([1, 2, 3] )); //[]