1// 1. filter()
2function removeDuplicates(array) {
3 return array.filter((a, b) => array.indexOf(a) === b)
4};
5// 2. forEach()
6function removeDuplicates(array) {
7 let x = {};
8 array.forEach(function(i) {
9 if(!x[i]) {
10 x[i] = true
11 }
12 })
13 return Object.keys(x)
14};
15// 3. Set
16function removeDuplicates(array) {
17 array.splice(0, array.length, ...(new Set(array)))
18};
19// 4. map()
20function removeDuplicates(array) {
21 let a = []
22 array.map(x =>
23 if(!a.includes(x) {
24 a.push(x)
25 })
26 return a
27};
28/THIS SITE/ "https://dev.to/mshin1995/back-to-basics-removing-duplicates-from-an-array-55he#comments"
1// JavaScript - finds if there is duplicate in an array.
2// Returns True or False.
3
4const isThereADuplicate = function(arrayOfNumbers) {
5 // Create an empty associative array or hash.
6 // This is preferred,
7 let counts = {};
8 // // but this also works. Comment in below and comment out above if you want to try.
9 // let counts = [];
10
11 for(var i = 0; i <= arrayOfNumbers.length; i++) {
12 // As the arrayOfNumbers is being iterated through,
13 // the counts hash is being populated.
14 // Each value in the array becomes a key in the hash.
15 // The value assignment of 1, is there to complete the hash structure.
16 // Once the key exists, meaning there is a duplicate, return true.
17 // If there are no duplicates, the if block completes and returns false.
18 if(counts[arrayOfNumbers[i]] === undefined) {
19 counts[arrayOfNumbers[i]] = 1;
20 } else {
21 return true;
22 }
23 }
24 return false;
25}