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);
1var input = [1, 2, 3, 1, 3, 1];
2
3var duplicates = input.reduce(function(acc, el, i, arr) {
4 if (arr.indexOf(el) !== i && acc.indexOf(el) < 0) acc.push(el); return acc;
5}, []);
6
7document.write(duplicates); // = 1,3 (actual array == [1, 3])
1const names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
2
3const count = names =>
4 names.reduce((a, b) => ({ ...a,
5 [b]: (a[b] || 0) + 1
6 }), {}) // don't forget to initialize the accumulator
7
8const duplicates = dict =>
9 Object.keys(dict).filter((a) => dict[a] > 1)
10
11console.log(count(names)) // { Mike: 1, Matt: 1, Nancy: 2, Adam: 1, Jenny: 1, Carl: 1 }
12console.log(duplicates(count(names))) // [ 'Nancy' ]
1const arr = ["q", "w", "w", "e", "i", "u", "r"]
2arr.reduce((acc, cur) => {
3 if(acc[cur]) {
4 acc.duplicates.push(cur)
5 } else {
6 acc[cur] = true //anything could go here
7 }
8}, { duplicates: [] })
1let a = [1, 2, 3, 4, 2, 2, 4, 1, 5, 6]
2let b = [...new Set(a.sort().filter((o, i) => o !== undefined && a[i + 1] !== undefined && o === a[i + 1]))]
3
4// b is now [1, 2, 4]
1var names = ['Mike', 'Matt', 'Nancy', 'Adam', 'Jenny', 'Nancy', 'Carl']
2
3var uniq = names
4 .map((name) => {
5 return {
6 count: 1,
7 name: name
8 }
9 })
10 .reduce((a, b) => {
11 a[b.name] = (a[b.name] || 0) + b.count
12 return a
13 }, {})
14
15var duplicates = Object.keys(uniq).filter((a) => uniq[a] > 1)
16
17console.log(duplicates) // [ 'Nancy' ]