Ad
Code
Diff
  • #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cassert>
    
    int unique_sum(const std::vector<int>& n) {
      auto nums = n;
      std::sort(nums.begin(), nums.end(), [](int a, int b) {
        return a < b;
      });
      int sum = 0;
      for(size_t i = 0; i < nums.size(); i++) {
        if(nums[i] == nums[i+1] || nums[i] == nums[i-1]) {
          continue;
        } else {
          sum += nums[i];
        }
      }
      return sum;
    }
    • #include <iostream>
    • #include <vector>
    • #include <unordered_map>
    • #include <algorithm>
    • #include <cassert>
    • int unique_sum(const std::vector<int>& nums) {
    • std::unordered_map<int, int> hashMap;
    • for (int i = 0; i < nums.size(); i++) {
    • if (hashMap.find(nums[i]) == hashMap.end()) {
    • hashMap[nums[i]] = nums[i];
    • int unique_sum(const std::vector<int>& n) {
    • auto nums = n;
    • std::sort(nums.begin(), nums.end(), [](int a, int b) {
    • return a < b;
    • });
    • int sum = 0;
    • for(size_t i = 0; i < nums.size(); i++) {
    • if(nums[i] == nums[i+1] || nums[i] == nums[i-1]) {
    • continue;
    • } else {
    • hashMap[nums[i]] = 0;
    • sum += nums[i];
    • }
    • }
    • int sum = 0;
    • for (auto i: hashMap) {
    • sum = sum + hashMap[i.first];
    • }
    • return sum;
    • }
Test Cases
Diff
  • // TODO: Replace examples and use TDD by writing your own tests
    
    Describe(unique_sum_of_numbers)
    {
        It(should_do_something)
        {
          Assert::That(unique_sum({1,2,3,2}), Equals(4));
            Assert::That(unique_sum({1,1,2,3}), Equals(5));
              Assert::That(unique_sum({-1,-1,0,1}), Equals(1));
        };
        It(should_do_something_other)
        {
          Assert::That(unique_sum({}), Equals(0));
            Assert::That(unique_sum({5, 5, 5, 5}), Equals(0));
              Assert::That(unique_sum({10, 20, 30, 20, 10}), Equals(30));
                Assert::That(unique_sum({1, 2, 3, 4, 5}), Equals(15));
                  Assert::That(unique_sum({-5, -5, 0, 5, 5}), Equals(0));
                    Assert::That(unique_sum({-1, 2, -1, 3, 4, 2}), Equals(7));
                      Assert::That(unique_sum({1, 2, 3, 4, 5, 1, 2, 3}), Equals(9));
                        Assert::That(unique_sum({100, 200, 300, 300, 200, 100}), Equals(0));
        }
    };
    • // TODO: Replace examples and use TDD by writing your own tests
    • Describe(unique_sum_of_numbers)
    • {
    • It(should_do_something)
    • {
    • Assert::That(unique_sum({1,2,3,2}), Equals(4));
    • Assert::That(unique_sum({1,1,2,3}), Equals(5));
    • Assert::That(unique_sum({-1,-1,0,1}), Equals(1));
    • };
    • It(should_do_something_other)
    • {
    • Assert::That(unique_sum({}), Equals(0));
    • Assert::That(unique_sum({5, 5, 5, 5}), Equals(0));
    • Assert::That(unique_sum({10, 20, 30, 20, 10}), Equals(30));
    • Assert::That(unique_sum({1, 2, 3, 4, 5}), Equals(15));
    • Assert::That(unique_sum({-5, -5, 0, 5, 5}), Equals(0));
    • Assert::That(unique_sum({-1, 2, -1, 3, 4, 2}), Equals(7));
    • Assert::That(unique_sum({1, 2, 3, 4, 5, 1, 2, 3}), Equals(9));
    • Assert::That(unique_sum({100, 200, 300, 300, 200, 100}), Equals(0));
    • }