- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sum all duplicate values in array in JavaScript
We are required to write a JavaScript function that takes in an array of numbers with duplicate entries and sums all the duplicate entries to one index.
Example
The code for this will be −
const input = [1, 3, 1, 3, 5, 7, 5, 3, 4]; const sumDuplicate = arr => { const map = arr.reduce((acc, val) => { if(acc.has(val)){ acc.set(val, acc.get(val) + 1); }else{ acc.set(val, 1); }; return acc; }, new Map()); return Array.from(map, el => el[0] * el[1]); }; console.log(sumDuplicate(input));
Output
The output in the console −
[ 2, 9, 10, 7, 4 ]
- Related Articles
- Sum all duplicate value in array - JavaScript
- List all duplicate values in a nested JavaScript object
- Removing duplicate values in a twodimensional array in JavaScript
- How to find duplicate values in a JavaScript array?
- How to remove duplicate property values in array – JavaScript?
- Make an array of another array's duplicate values in JavaScript
- Finding all duplicate numbers in an array with multiple duplicates in JavaScript
- Filter unique array values and sum in JavaScript
- Sum all similar elements in one array - JavaScript
- Sum from array values with similar key in JavaScript
- Sum of nested object values in Array using JavaScript
- Sum of all prime numbers in an array - JavaScript
- Sum of all positives present in an array in JavaScript
- JavaScript reduce sum array with undefined values
- Sum of array object property values in new array of objects in JavaScript

Advertisements