Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
Selected Reading
Sum identical elements within one array in JavaScript
We are required to write a JavaScript function that takes in an array of Numbers.
The array might contain some repeating / duplicate entries within it. Our function should add all the duplicate entries and return the new array thus formed.
Example
The code for this will be −
const arr = [20, 20, 20, 10, 10, 5, 1];
const sumIdentical = (arr = []) => {
let map = {};
for (let i = 0; i Output
And the output in the console will be −
[ 1, 5, 20, 60 ]
Advertisements
