- 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 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
- Related Articles
- Product of non-repeating (distinct) elements in an Array in C++
- Find sum of non-repeating (distinct) elements in an arrays in C++
- Finding the largest non-repeating number in an array in JavaScript
- Sum of distinct elements of an array - JavaScript
- Sum of distinct elements of an array in JavaScript
- JavaScript construct an array with elements repeating from a string
- Sorting array of exactly three unique repeating elements in JavaScript
- Detecting the first non-repeating string in Array in JavaScript
- Sum of all prime numbers in an array - JavaScript
- How to find the sum of all elements of a given array in JavaScript?
- Sum all similar elements in one array - JavaScript
- Building frequency map of all the elements in an array JavaScript
- Return an array of all the indices of minimum elements in the array in JavaScript
- Absolute sum of array elements - JavaScript
- Thrice sum of elements of array - JavaScript

Advertisements