Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions leetcode/hash-table/sum-of-unique-elements/anunihu-james.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
var sumOfUnique = function (nums) {
let resultArr = []; // Empty array to store unique numbers
for (const num of nums) { // Loop through the array
if (nums.indexOf(num) === nums.lastIndexOf(num)) { // Conditional statement
resultArr.push(num); // Push unique numbers into the empty array
}
}
if (resultArr.length === 0) {
return 0;
} else {
return resultArr.reduce((a, b) => a + b); // returns the sum of the numbers in the result array
}
};

// The time complexity is O(N) and the space complexity is also O(N).