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 andlet chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });
Code language: JavaScript (javascript)6The
let chars = ['A', 'B', 'A', 'C', 'B']; chars.forEach((c, index) => { console.log(`${c} - ${index} - ${chars.indexOf(c)}`); });
Code language: JavaScript (javascript)7 returnslet 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 orlet 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)0Output:
[ 'A', 'B', 'C' ]
Code language: JSON / JSON with Comments (json)14) 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)2The 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)3Output:
[ 'A', 'B', 'C' ]
Code language: JSON / JSON with Comments (json)4How 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)5It 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)6Second, remove the duplicate by creating a new Map() object:
[ 'A', 'B', 'C' ]
Code language: JSON / JSON with Comments (json)7Output:
[ 'A', 'B', 'C' ]
Code language: JSON / JSON with Comments (json)8Because 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)9Output:
let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');
Code language: JavaScript (javascript)0Finally, 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)1Output:
[ 'A', 'B', 'C' ]
Code language: JSON / JSON with Comments (json)4Put it all together:
let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');
Code language: JavaScript (javascript)3The 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)5So:
let chars = ['A', 'B', 'A', 'C', 'B']; chars.indexOf('B');
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)7For 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 theA - 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)85) 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)9How it works.
First, return the same array if it has zero or one element:
1
0Second, return the input array if the callback is not passed:
1
1Third, 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 theA - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1
4 method.1
2The 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 bothA - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1
6 andA - 0 - 0 B - 1 - 1 A - 2 - 0 C - 3 - 3 B - 4 - 1
7 properties: