1var array = [1, 2, 2, 3, 3, 4, 5, 6, 2, 3, 7, 8, 5, 22, 1, 2, 511, 12, 50, 22];
2
3console.log([...new Set(
4 array.filter((value, index, self) => self.indexOf(value) !== index))]
5);
1const findDuplicates = (arr) => {
2 let sorted_arr = arr.slice().sort(); // You can define the comparing function here.
3 // JS by default uses a crappy string compare.
4 // (we use slice to clone the array so the
5 // original array won't be modified)
6 let results = [];
7 for (let i = 0; i < sorted_arr.length - 1; i++) {
8 if (sorted_arr[i + 1] == sorted_arr[i]) {
9 results.push(sorted_arr[i]);
10 }
11 }
12 return results;
13}
14
15let duplicatedArray = [9, 4, 111, 2, 3, 9, 4, 5, 7];
16console.log(`The duplicates in ${duplicatedArray} are ${findDuplicates(duplicatedArray)}`);
1function doubleValues(array) {
2 var newArray = [];
3 array.forEach(function (el) { newArray.push(el, el); });
4 return newArray;
5}
6
7console.log(doubleValues([1,2,3]));