CodexBloom - Programming Q&A Platform

How can I efficiently find duplicates in a large array of strings in JavaScript?

👀 Views: 455 đŸ’Ŧ Answers: 1 📅 Created: 2025-06-11
javascript arrays performance JavaScript

I'm having trouble with Hey everyone, I'm running into an issue that's driving me crazy... I'm currently working on a project where I need to identify duplicate entries in a large array of strings. The array can contain over a million entries, and I'm trying to find a way to do this efficiently without running into performance optimization. I've tried using a simple nested loop approach, but it gets incredibly slow as the size of the array increases. Here's the code I've tried so far: ```javascript const findDuplicates = (arr) => { const 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; }; const largeArray = [...Array(1000000)].map(() => Math.random().toString(36).substring(2, 15)); const result = findDuplicates(largeArray); console.log(result); ``` While this works for smaller arrays, I've noticed that it takes too long and sometimes even throws a "Maximum call stack size exceeded" behavior when the array is too large. I've considered using a `Set` to optimize the lookup, but I'm unsure how to implement it effectively. Could someone provide guidance on a more efficient algorithm or method to find duplicates in a large array without sacrificing performance? Any help would be greatly appreciated! This is happening in both development and production on Ubuntu 22.04. Has anyone else encountered this? This is my first time working with Javascript 3.11. Any examples would be super helpful. This issue appeared after updating to Javascript latest. Any examples would be super helpful. This is part of a larger mobile app I'm building. Could someone point me to the right documentation? Thanks for any help you can provide!