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
-
Economics & Finance
Sum of all the non-repeating elements of an array JavaScript
To find the sum of non-repeating elements in an array, we need to identify elements that appear exactly once and add them together.
Problem Statement
Given an array of numbers, we want to calculate the sum of all elements that appear only once in the array.
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
console.log("Input array:", arr);
Input array: [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14]
In this array, the non-repeating elements are: 23, 24, 33, 44, 87. Their sum is 23 + 24 + 33 + 44 + 87 = 211.
Method 1: Using indexOf and lastIndexOf
We can check if an element appears only once by comparing its first and last occurrence positions:
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
const nonRepeatingSum = arr => {
let sum = 0;
for(let i = 0; i < arr.length; i++){
// If first occurrence equals last occurrence, element appears only once
if(arr.indexOf(arr[i]) === arr.lastIndexOf(arr[i])){
sum += arr[i];
}
}
return sum;
};
console.log("Sum of non-repeating elements:", nonRepeatingSum(arr));
Sum of non-repeating elements: 211
Method 2: Using Map to Count Frequencies
A more efficient approach uses a Map to count element frequencies, then sum elements with count of 1:
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
const nonRepeatingSumWithMap = arr => {
const frequencyMap = new Map();
// Count frequencies
for(let num of arr){
frequencyMap.set(num, (frequencyMap.get(num) || 0) + 1);
}
// Sum elements with frequency 1
let sum = 0;
for(let [num, count] of frequencyMap){
if(count === 1){
sum += num;
}
}
return sum;
};
console.log("Sum using Map approach:", nonRepeatingSumWithMap(arr));
Sum using Map approach: 211
Method 3: Using Filter and Includes
We can also filter the array to get unique elements first:
const arr = [14, 54, 23, 14, 24, 33, 44, 54, 77, 87, 77, 14];
const nonRepeatingSumFilter = arr => {
return arr
.filter((num, index) => arr.indexOf(num) === arr.lastIndexOf(num))
.reduce((sum, num) => sum + num, 0);
};
console.log("Sum using filter approach:", nonRepeatingSumFilter(arr));
// Let's also see which elements are non-repeating
const nonRepeatingElements = arr.filter((num, index) =>
arr.indexOf(num) === arr.lastIndexOf(num)
);
console.log("Non-repeating elements:", nonRepeatingElements);
Sum using filter approach: 211 Non-repeating elements: [23, 24, 33, 44, 87]
Comparison
| Method | Time Complexity | Space Complexity | Readability |
|---|---|---|---|
| indexOf/lastIndexOf | O(n²) | O(1) | Good |
| Map frequency | O(n) | O(n) | Good |
| Filter + reduce | O(n²) | O(n) | Excellent |
Conclusion
The Map-based approach offers the best time complexity O(n), while the filter method provides the most readable code. Choose based on your performance requirements and code style preferences.
