JavaScript Program to Find Duplicate Elements in an Array Last Updated : 07 Apr, 2025 Comments Improve Suggest changes Like Article Like Report Here are the different methods to find duplicate elements in the array1. Using Nested For In LoopThe 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); Output[ 2, 3 ] In this exampleThe 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() MethodThe 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); Output[ 1, 2, 3 ] In this exampleThe 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() MethodIn 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); Output[ 2, 3 ] In this examplefilter() 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 SetA 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); Output[ 2, 3 ] In this exampleWe 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() MethodThe 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); Output[ 2, 3 ] In this exampleThe 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 StructureMap() 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); Output[ 2, 3 ] In this exampleWe 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. Comment More infoAdvertise with us Next Article JavaScript Program to Find Duplicate Elements in an Array P prathamgfg Follow Improve Article Tags : JavaScript Web Technologies javascript-array JavaScript-DSA JavaScript-Program +1 More 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 Count Unequal Element Pairs from the Given Array Unequal element pairs in an array refer to pairs of distinct elements within the array that have different values. These pairs consist of two elements that are not equal to each other, highlighting their inequality when compared in the context of the array's values.Examples:Input: arr[] = {6, 5, 2, 4 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 PHP Program to Find Duplicate Elements from an Array Given an array containing some repeated elements, the task is to find the duplicate elements from the given array. If array elements are not repeated, then it returns an empty array.Examples:Input: arr = [1, 2, 3, 6, 3, 6, 1]Output: [1, 3, 6]Input: arr = [3, 5, 2, 5, 3, 9]Output: [3, 5]There are two 6 min read Javascript Program for Find lost element from a duplicated array Given two arrays that are duplicates of each other except one element, that is one element from one of the array is missing, we need to find that missing element.Examples: Input: arr1[] = {1, 4, 5, 7, 9} arr2[] = {4, 5, 7, 9}Output: 11 is missing from second array.Input: arr1[] = {2, 3, 4, 5} arr2[] 4 min read JavaScript Program to Print All Distinct Elements in an Integer Array Given an Array of Integers consisting of n elements with repeated elements, the task is to print all the distinct elements of the array using JavaScript. We can get the distinct elements by creating a set from the integer array. Examples: Input : arr = [ 1, 2, 3, 4, 4, 5, 5, 6, 7, 7, 8, 9, 9 ] Outpu 3 min read Like