Find duplicate items in an array in JavaScript
This post will discuss how to find duplicate items in an array in JavaScript.
Finding duplicate items in an array in JavaScript is a common task that can be done in various ways. Some of the functions are:
1. Using nested loops
We can use nested loops to compare each element of the array with every other element and compares each element with the rest of the elements using a comparison operator (such as ===). If the elements are equal, it means they are duplicates. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function findDuplicates(arr) { let duplicates = new Set(); for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j]) { duplicates.add(arr[i]); } } } return [...duplicates]; } let arr = [1, 2, 3, 2, 4, 2, 5, 5, 6]; let duplicates = findDuplicates(arr); console.log(duplicates); // Output: [2, 5] |
This function is compatible with older browsers, but it is not very efficient or elegant for large arrays, as it has a quadratic time complexity. Here’s another approach without using the Set data structure. In this example, the nested loops compare each element of the array with every other element. If a duplicate is found and it hasn’t been added to the duplicates array yet, it is added.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
function findDuplicates(arr) { let duplicates = []; for (let i = 0; i < arr.length; i++) { for (let j = i + 1; j < arr.length; j++) { if (arr[i] === arr[j] && !duplicates.includes(arr[i])) { duplicates.push(arr[i]); } } } return duplicates; } let arr = [1, 2, 3, 2, 4, 2, 5, 5, 6]; let duplicates = findDuplicates(arr); console.log(duplicates); // Output: [2, 5] |
2. Using indexOf() and filter() functions
This method uses the indexOf() function to find the first occurrence of an element in the array and the filter() function to create a new array with the elements that pass a test implemented by a function. The function checks if the index of the current element is not equal to the index of its first occurrence, which means it is a duplicate. Here’s an example:
|
1 2 3 4 5 6 7 8 9 |
function findDuplicates(arr) { // filter out elements having different index from their first occurrence in the array let duplicates = arr.filter((element, index) => index !== arr.indexOf(element)); return duplicates; } let arr = [1, 2, 3, 2, 4, 2, 5, 5, 6]; let duplicates = findDuplicates(arr); console.log(duplicates); // Output: [2, 2, 5] |
This function is simple and elegant, but it may not be very efficient for large arrays, as it has a quadratic time complexity. It also requires ES6 support or a polyfill for older browsers. It may also result in duplicate elements in the duplicates array.
3. Using a Set and filter() functions
This method uses the Set object to store the unique elements of the array in a set data structure, which allows fast lookup of values. Then it uses the filter() function to create a new array with the elements of the original array that are not in the set, which means they are duplicates. Here’s an example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
function findDuplicates(arr) { let set = new Set(); let duplicates = arr.filter(item => { if (set.has(item)) { return true; } else { set.add(item); return false; } }); return duplicates; } let arr = [1, 2, 3, 2, 4, 2, 5, 5, 6]; let duplicates = findDuplicates(arr); console.log(duplicates); // Output: [2, 2, 5] |
This function is more efficient than the previous functions, as it has a linear time complexity. However, it also requires ES6 support or a polyfill for older browsers. It may also result in duplicate elements in the duplicates array.
These are some of the ways to find duplicate items in an array in JavaScript. We can choose any of them based on our preference and use case.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)