Skip to content

Commit

Permalink
Added Unique Number of Occurrences
Browse files Browse the repository at this point in the history
  • Loading branch information
Ace-Krypton committed Sep 30, 2023
1 parent 3f55be2 commit 8328c1f
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 0 deletions.
11 changes: 11 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1140,4 +1140,15 @@ add_executable(FindTheDifferenceOfTwoArrays
target_link_libraries(
FindTheDifferenceOfTwoArrays
GTest::gtest_main
)

# Unique Number of Occurrences
add_executable(UniqueNumberOfOccurrences
Easy/UniqueNumberOfOccurrences/include/solution.hpp
Easy/UniqueNumberOfOccurrences/tests/test.cpp
)

target_link_libraries(
UniqueNumberOfOccurrences
GTest::gtest_main
)
19 changes: 19 additions & 0 deletions Easy/UniqueNumberOfOccurrences/include/solution.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include <vector>
#include <iostream>
#include <algorithm>
#include <gtest/gtest.h>

class Solution {
public:
static auto unique_occurrences(const std::vector<int32_t> &arr) -> bool {
std::set<int32_t> freq;
std::unordered_map<int32_t, size_t> mp;

for (const auto &element : arr) mp[element]++;
for (const auto &val : mp) freq.insert(val.second);

return freq.size() == mp.size();
}
};
39 changes: 39 additions & 0 deletions Easy/UniqueNumberOfOccurrences/tests/test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#include "../include/solution.hpp"

class UniqueNumberOfOccurrences : public ::testing::Test {
protected:
~UniqueNumberOfOccurrences() override = default;
};

TEST_F(UniqueNumberOfOccurrences, FirstTest) {
std::vector<int32_t> elements = {1, 2};

bool result = Solution::unique_occurrences(elements);
bool expected = false;

ASSERT_EQ(result, expected);
}

TEST_F(UniqueNumberOfOccurrences, SecondTest) {
std::vector<int32_t> elements = {1, 2, 2, 1, 1, 3};

bool result = Solution::unique_occurrences(elements);
bool expected = true;

ASSERT_EQ(result, expected);
}

TEST_F(UniqueNumberOfOccurrences, ThirdTest) {
std::vector<int32_t> elements =
{-3, 0, 1, -3, 1, 1, 1, -3, 10, 0};

bool result = Solution::unique_occurrences(elements);
bool expected = true;

ASSERT_EQ(result, expected);
}

auto main(int argc, char **argv) -> int {
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

0 comments on commit 8328c1f

Please sign in to comment.