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


In the given problem statement we have to find all duplicate numbers in an array with multiple duplicates with the help of Javascript functionalities. So we will use basic Javascript tot solve this problem.

Understanding the Problem

The problem at hand is to find the duplicate numbers in an array with the help of JavaScript. So we will basically extract all the numbers which are appearing more than once in the given array. And in response we will get the array of repeated numbers. For example, suppose we have an array as [1, 1, 4, 8, 2, 2, 6, 6, 6]. So in this array the repeated or duplicate items are [1, 2, 6]. So this array will be the output of the function.

Logic for the given Problem

To solve the given problem we will use an object to keep record of the count of every number seen. So we will traverse through the array and for each item we will verify the condition is the item is already present in the object. And if it is already present then we will increment the count with 1. Otherwise we add that item to the object with an initial count to one. And as a result we have the numbers from the object that have a count more than one, which is the required array of duplicate numbers.

Algorithm

Step 1: As we have to find the duplicate numbers in the given array so for solving this problem we will define a function and give it a name as findDuplicates and in this function we will pass a parameter array. This array will contain duplicate items in it.

Step 2: After creating the above function we will create a blank hash map or object to store the duplicate number counts. And also create an array with empty values in it. And name it as duplicates and this array will store the duplicate numbers of the input array.

Step 3: Now we are inside the function in which we have already defined hashmap to store the duplicate numbers information. So after that we will iterate every element in the array. And then check if the item exists as a key in the hash map.

Step 4: So in the hashmap if the key is present then we will increment the count by one. Otherwise we will add the item to the hashmap with the initial count of one.

Step 5: In this step we will use another for loop to traverse over the hash map key-value pairs.

Step 6: And then we will check the condition if the count for the number is greater than one so we will add it to the result array.

Example

//Function to find the duplicate items
function findDuplicates(array) {
   const countMap = {};
   const duplicates = [];

   for (let i = 0; i < array.length; i++) {
      const num = array[i];
      countMap[num] = (countMap[num] || 0) + 1;
   }

   for (const num in countMap) {
      if (countMap[num] > 1) {
         duplicates.push(Number(num));
      }
   }

   return duplicates;
}

//Usage of the function
const numbers = [1, 2, 3, 4, 4, 5, 6, 6, 7, 8, 8, 8];
const result = findDuplicates(numbers);
console.log(result);

Output

[ 4, 6, 8 ]

Complexity

The time complexity to find all the duplicate numbers in an array is O(n), in which n is the size of the given input array. Because we have iterated over the given array only once to update the count map. And the space complexity for this approach is also O(n) as the count map can contain all distinct numbers in the worst case.

Conclusion

So we have found all the duplicate numbers with multiple numbers with the help of a hash map or object for keeping record of the counts. With the help of the function we can easily find out the repeated items in the array and create a new set of duplicate items.

Updated on: 14-Aug-2023

547 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements