From fd576c9e59e141f0f01f466ac2bcb68a0bb3d914 Mon Sep 17 00:00:00 2001 From: Chukwuebukaj Date: Thu, 13 Oct 2022 22:47:37 +0100 Subject: [PATCH] I added the sum-of-unique-elements solution --- .../sum-of-unique-elements/anunihu-james.js | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 leetcode/hash-table/sum-of-unique-elements/anunihu-james.js diff --git a/leetcode/hash-table/sum-of-unique-elements/anunihu-james.js b/leetcode/hash-table/sum-of-unique-elements/anunihu-james.js new file mode 100644 index 0000000..012dded --- /dev/null +++ b/leetcode/hash-table/sum-of-unique-elements/anunihu-james.js @@ -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).