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 of all the non-repeating elements of an array JavaScript
Suppose, we have an array of numbers like this −
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
We are required to write a JavaScript function that takes in one such array and counts the sum of all the elements of the array that appear only once in the array −
For example:
The output for the array mentioned above will be −
356
The code for this will be −
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
const nonRepeatingSum = arr => {
let res = 0;
for(let i = 0; i < arr.length; i++){
if(i !== arr.lastIndexOf(arr[i])){
continue;
};
res += arr[i];
};
return res;
};
console.log(nonRepeatingSum(arr));
Following is the output on console −
30
Advertisements
