JavaScript Program to Find Duplicate Elements in an Array
Last Updated :
07 Apr, 2025
Here are the different methods to find duplicate elements in the array
1. Using Nested For In Loop
The for...in loop is typically used to iterate over the keys of an object or the indices of an array.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let dupli = [];
for (let i in a) {
for (let j in a) {
if (i !== j && a[i] === a[j] && !dupli.includes(a[i])) {
dupli.push(a[i]);
}
}
}
console.log(dupli);
In this example
- The code uses nested for...in loops to compare each element in the array with all others.
- If a duplicate is found (and not already in the duplicates array), it is added to the list, resulting in [2, 3].
2. Using Sort() Method
The array.sort() method is provided by Javascript by which we can sort our array, and after sorting the array, we are checking that the element at the last index is the same or not.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let dupli = [];
arr.sort();
for (let i = 0; i < a.length - 1; i++) {
if (a[i] === a[i + 1] && !dupli.includes(a[i])) {
dupli.push(a[i]);
}
}
console.log(dupli);
In this example
- The sort() method is used to sort the array in ascending order.
- After sorting, we simply check if any consecutive elements are the same.
- If a duplicate is found and it's not already in the duplicates array, it is added.
3. Using filter() and indexOf() Method
In JavaScript, array methods such as filter() and indexOf() can be used to find duplicates in a concise way.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let dupli = a.filter((value, index) =>
a.indexOf(value) !== index && a.lastIndexOf(value) === index);
console.log(dupli);
In this example
- filter() creates a new array containing elements that satisfy a condition.
- arr.indexOf(value) returns the first occurrence index of value.
- arr.indexOf(value) !== index checks if the current element's first occurrence is not at the current index (indicating a duplicate).
- arr.indexOf(value) === index ensures that we don’t filter elements that are encountered for the first time.
4. Using a Set
A Set in JavaScript is a collection of unique values. Using this property, we can easily identify duplicates by comparing the current element with the values already stored in the set.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let seen = new Set();
let dupli = [];
for (let i = 0; i < a.length; i++) {
if (seen.has(a[i])) {
dupli.push(a[i]);
} else {
seen.add(a[i]);
}
}
console.log(dupli);
In this example
- We use a Set called seen to store unique elements as we iterate over the array.
- When an element is encountered that already exists in the Set, it is a duplicate, so we add it to the duplicates array.
- If the element is not in the Set, we add it to seen.
5. Using reduce() and includes() Method
The reduce() method can also be used to find duplicates. It iterates over the array and accumulates the result in an array, checking for duplicates along the way.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let dupli = [];
a.reduce((acc, curr) => {
if (acc.indexOf(curr) === -1 && a.indexOf(curr) !== a.lastIndexOf(curr)) {
acc.push(curr);
}
return acc;
}, dupli);
console.log(dupli);
In this example
- The code uses reduce() to accumulate duplicates in the dupli array by checking if an element appears more than once in the array (a.indexOf(curr) !== a.lastIndexOf(curr)) and ensuring it's added only once (acc.indexOf(curr) === -1). The result is [2, 3].
6. Using a Map Data Structure
Map() method is used to store the frequency of each element in the array. Elements with a frequency greater than 1 are considered duplicates.
JavaScript
const a = [1, 2, 3, 4, 5, 2, 3, 6];
let map = new Map();
let dupli = [];
for (let i = 0; i < a.length; i++) {
if (map.has(a[i])) {
map.set(a[i], map.get(a[i]) + 1);
} else {
map.set(a[i], 1);
}
}
for (let [key, val] of map) {
if (val > 1) {
dupli.push(key);
}
}
console.log(dupli);
In this example
- We use a Map to store each element of the array as the key and its frequency as the value.
- If the element is already in the Map, we increment its count. If it’s not in the Map, we add it with a count of 1.
- After iterating through the array, we check the Map to find elements with a frequency greater than 1, indicating duplicates.
Similar Reads
JavaScript Program to Remove Duplicate Elements From a Sorted Array Given a sorted array arr[] of size N, the task is to remove the duplicate elements from the array. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Explanation: All the elements are 2, So only keep one instance of 2. Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4,
3 min read
JavaScript Program to Find the First Non-Repeated Element in an Array Finding the first non-repeated element in an array refers to identifying the initial occurrence of an element that does not occur again elsewhere within the array, indicating uniqueness among the elements.Examples: Input: {-1, 2, -1, 3, 0}Output: 2Explanation: The first number that does not repeat i
5 min read
JavaScript Program to Check if an Array Contains only Unique Values In this article, we are given an array, Our task is to find whether the elements in an array are unique or not.Examples:Input 1: 7,8,1,5,9 Output: true Input2: 7,8,1,5,5 Output: falseIn Input 1, elements 7,8,1,5,9 are distinct from each other and they were unique, there was no repetition of elements
4 min read
Checking for Duplicate Strings in JavaScript Array Checking for duplicate strings in a JavaScript array involves identifying if there are any repeated string values within the array. Given an array of strings a with size N, find all strings that occur more than once. If none is found, return [-1]. Example: Input: N = 4arr = ['apple', 'banana', 'oran
3 min read
JavaScript Program to Find Common Elements Between Two Sorted Arrays using Binary Search Given two sorted arrays, our task is to find common elements in two sorted arrays then the program should return an array that contains all the elements that are common to the two arrays. The arrays can contain duplicate elements, that is, values at one index in an array can be equal to the values a
3 min read
Javascript Program for Last duplicate element in a sorted array We have a sorted array with duplicate elements and we have to find the index of last duplicate element and print index of it and also print the duplicate element. If no such element found print a message. Examples: Input : arr[] = {1, 5, 5, 6, 6, 7}Output :Last index: 4Last duplicate item: 6Input :
3 min read