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 all similar elements in one array - JavaScript
We are required to write a JavaScript function that takes in an array of Numbers and sums all the identical numbers together at one index
For example −
If the input array is −
const arr = [20, 10, 15, 20, 15, 10];
Then the output should be −
const output = [40, 20, 30];
Example
Following is the code −
const arr = [20, 10, 15, 20, 15, 10];
const addSimilar = arr => {
for(let i = 0; i Output
This will produce the following output in console −
[ 40, 20, 30 ]
Advertisements
