Finding all duplicate numbers in an array with multiple duplicates in JavaScript

In JavaScript, finding duplicate numbers in an array is a common task that can be efficiently solved using objects to track element frequencies. This approach helps identify all numbers that appear more than once in the array.

Understanding the Problem

We need to find all duplicate numbers in an array that may contain multiple duplicates. For example, given the array [1, 1, 4, 8, 2, 2, 6, 6, 6], the duplicate numbers are [1, 2, 6] since these appear more than once.

Using Object to Count Frequencies

The most efficient approach uses an object to count occurrences of each number, then filters numbers with counts greater than 1.

function findDuplicates(array) {
   const countMap = {};
   const duplicates = [];

   // Count occurrences of each number
   for (let i = 0; i < array.length; i++) {
      const num = array[i];
      countMap[num] = (countMap[num] || 0) + 1;
   }

   // Find numbers with count > 1
   for (const num in countMap) {
      if (countMap[num] > 1) {
         duplicates.push(Number(num));
      }
   }

   return duplicates;
}

// Example usage
const numbers = [1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8];
const result = findDuplicates(numbers);
console.log("Duplicate numbers:", result);
Duplicate numbers: [ 4, 6, 8 ]

Using Map for Better Performance

Using JavaScript's Map object can provide better performance and cleaner code:

function findDuplicatesWithMap(array) {
   const countMap = new Map();
   const duplicates = [];

   // Count occurrences
   for (const num of array) {
      countMap.set(num, (countMap.get(num) || 0) + 1);
   }

   // Find duplicates
   for (const [num, count] of countMap) {
      if (count > 1) {
         duplicates.push(num);
      }
   }

   return duplicates;
}

// Test with different array
const testArray = [5, 1, 2, 1, 3, 5, 5, 2];
console.log("Duplicates:", findDuplicatesWithMap(testArray));
Duplicates: [ 5, 1, 2 ]

Using Set for One-Pass Solution

For a more elegant approach, we can use Sets to track seen and duplicate elements:

function findDuplicatesWithSet(array) {
   const seen = new Set();
   const duplicates = new Set();

   for (const num of array) {
      if (seen.has(num)) {
         duplicates.add(num);
      } else {
         seen.add(num);
      }
   }

   return Array.from(duplicates);
}

// Example usage
const numbers2 = [9, 3, 9, 3, 9, 7, 9];
console.log("Duplicates found:", findDuplicatesWithSet(numbers2));
Duplicates found: [ 9, 3 ]

Comparison of Methods

Method Time Complexity Space Complexity Advantages
Object counting O(n) O(n) Simple, widely supported
Map counting O(n) O(n) Better performance, cleaner syntax
Set tracking O(n) O(n) One-pass solution, elegant

Conclusion

All three methods efficiently find duplicate numbers with O(n) time complexity. The Set-based approach is most elegant for simple duplicate detection, while object/Map counting provides more detailed frequency information.

Updated on: 2026-03-15T23:19:00+05:30

865 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements