
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Finding all duplicate numbers in an array with multiple duplicates in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers that contains many duplicate entries.
The function should prepare an array of all the elements that appear more than once in the array and return that array.
For example −
If the input array is −
const arr = [1, 3, 4, 3, 5, 4, 6, 8, 8];
Then the output array should be −
const output = [3, 4, 8];
Example
Following is the code −
const arr = [1, 3, 4, 3, 5, 4, 6, 8, 8]; const findDuplicates = (arr = []) => { let map = {}; let res = []; for(let i = 0; i < arr.length; i++) { if(map[arr[i]]) { if(map[arr[i]] === 1) { res.push(arr[i]); } map[arr[i]] = map[arr[i]] + 1; } else { map[arr[i]] = 1; }; }; return res; }; console.log(findDuplicates(arr));
Output
Following is the output on console −
[3, 4, 8]
- Related Questions & Answers
- Distance between 2 duplicate numbers in an array JavaScript
- Finding even length numbers from an array in JavaScript
- Finding all possible combinations from an array in JavaScript
- Finding all possible subsets of an array in JavaScript
- Find All Duplicates in an Array in C++
- Finding missing element in an array of numbers in JavaScript
- Finding all the longest strings from an array in JavaScript
- Sum all duplicate value in array - JavaScript
- Finding a number and its nth multiple in an array in JavaScript
- Finding all peaks and their positions in an array in JavaScript
- Smallest Common Multiple of an array of numbers in JavaScript
- Sum all duplicate values in array in JavaScript
- Product of all other numbers an array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- Remove duplicates and map an array in JavaScript
Advertisements