Find duplicate array JavaScript

If you want to remove the duplicates, there is a very simple way, making use of the Set data structure provided by JavaScript. It’s a one-liner:

const yourArrayWithoutDuplicates = [...new Set(yourArray)]

To find which elements are duplicates, you could use this “array without duplicates” we got, and and remove each item it contains from the original array content:

const yourArray = [1, 1, 2, 3, 4, 5, 5]

const yourArrayWithoutDuplicates = [...new Set(yourArray)]

let duplicates = [...yourArray]
yourArrayWithoutDuplicates.forEach((item) => {
  const i = duplicates.indexOf(item)
  duplicates = duplicates
    .slice(0, i)
    .concat(duplicates.slice(i + 1, duplicates.length))
})

console.log(duplicates) //[ 1, 5 ]

Another solution is to sort the array, and then check if the “next item” is same to the current item, and put it into an array:

const yourArray = [1, 1, 2, 3, 4, 5, 5]

let duplicates = []

const tempArray = [...yourArray].sort()

for (let i = 0; i < tempArray.length; i++) {
  if (tempArray[i + 1] === tempArray[i]) {
    duplicates.push(tempArray[i])
  }
}

console.log(duplicates) //[ 1, 5 ]

Note that this only works for primitive values, not objects. In the case of objects, you need a way to compare them.

The

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
2 method returns the index of the first occurrence of an element in an array. For example:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)

Output:

1

The duplicate item is the item whose index is different from its

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
0 value:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)

Output:

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

To remove the duplicates, you use the

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
4 method to include only elements whose indexes match their indexOf values:

let chars = ['A', 'B', 'A', 'C', 'B']; let uniqueChars = chars.filter((c, index) => { return chars.indexOf(c) === index; }); console.log(uniqueChars);

Code language: JavaScript (javascript)

Output:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)

To find the duplicate values, you need to reverse the condition:

let chars = ['A', 'B', 'A', 'C', 'B']; let dupChars = chars.filter((c, index) => { return chars.indexOf(c) !== index; }); console.log(dupChars);

Code language: JavaScript (javascript)

Output:

[ 'A', 'B' ]

Code language: JSON / JSON with Comments (json)

3) Remove duplicates from an array using let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); }); Code language: JavaScript (javascript)5 and let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); }); Code language: JavaScript (javascript)6

The

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
7 returns

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
8 if an element is in an array or

let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });

Code language: JavaScript (javascript)
9 if it is not.

The following example iterates over elements of an array and adds to a new array only elements that are not already there:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
0

Output:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
1

4) Remove duplicates from an array of objects by one property

Suppose you have the following array of objects:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
2

The id of the first is the same as the third element. To remove the duplicate from the people array, you can use the following:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
3

Output:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
4

How it works.

First, create a new array from the original array using the

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

0 method:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
5

It returns an array of arrays. Each nested array contains the value of the id and the corresponding object:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
6

Second, remove the duplicate by creating a new Map() object:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
7

Output:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
8

Because the keys of a Map object are unique, creating a Map from the array of array removes the duplicate object by key (id in this case).

Third, get the iterator of the Map’s entries by calling the values() method:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
9

Output:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
0

Finally, convert the iterator to an array by using the spread operator:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
1

Output:

[ 'A', 'B', 'C' ]

Code language: JSON / JSON with Comments (json)
4

Put it all together:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
3

The following four lines of code:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
4

…can be shorted into one:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
5

So:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
6

The following unique() function accepts an array of objects and returns the unique element by a property:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
7

For example, you can use the

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

1 function to remove duplicate elements from the

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

2 array like this:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
8

5) Remove duplicates from an array of objects by multiple properties

The following unique() funciton remove duplicate from an array of object. The duplicate logic is specified by a callback function:

let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');

Code language: JavaScript (javascript)
9

How it works.

First, return the same array if it has zero or one element:

1

0

Second, return the input array if the callback is not passed:

1

1

Third, iterate over the element of the input array twice and successively compare the first element with the other elements. If two elements cause the callback function (

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

3) to return true, remove that element from the array using the

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

4 method.

1

2

The following example uses the unique() function to remove duplicates from the

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

2 array by both

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

6 and

A - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1

7 properties:

How do you find duplicates in an array?

Duplicate elements can be found using two loops. The outer loop will iterate through the array from 0 to length of the array. The outer loop will select an element. The inner loop will be used to compare the selected element with the rest of the elements of the array.

How to filter duplicates in array JavaScript?

To remove this, you can use several functions such as filter(), set(), reduce(), forEach(), and indexof() method. Using these functions, you can remove duplicates from JavaScript arrays.

How to find duplicate elements in two arrays in JavaScript?

with three methods :.
filter to get an array of duplicate element..
map to recover only an array of one column..
some to describe how object in array should be compared const duplicates = array1. filter(value => array2. some(oneElement => oneElement. id === value. id)); const duplicatesId = array1. filter(value => array2..

How do I find duplicate elements in an array ES6?

Check if an array contains duplicates in JavaScript.
Using ES6 Set. The Set object, introduced in the ES6, can remove duplicate values from an array. ... .
Using Underscore/Lodash Library. If you don't want to use Set as an intermediate data structure, you can use the uniq() method from underscore. ... .
Using Array. prototype..