From 8d4eb729b4537309bbfe2b70d43dad43b3e97e10 Mon Sep 17 00:00:00 2001 From: Matej Focko Date: Wed, 17 Jan 2024 15:37:57 +0100 Subject: [PATCH] =?UTF-8?q?cpp:=20add=20=E2=80=9C1207.=20Unique=20Number?= =?UTF-8?q?=20of=20Occurrences=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Matej Focko --- cpp/unique-number-of-occurrences.cpp | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 cpp/unique-number-of-occurrences.cpp diff --git a/cpp/unique-number-of-occurrences.cpp b/cpp/unique-number-of-occurrences.cpp new file mode 100644 index 0000000..9c7c213 --- /dev/null +++ b/cpp/unique-number-of-occurrences.cpp @@ -0,0 +1,21 @@ +#include +#include +#include + +class Solution { + public: + bool uniqueOccurrences(const std::vector &arr) { + std::map freqs; + for (const auto &x : arr) { + ++freqs[x]; + } + + // get unique values + std::set unique_counts; + for (const auto &[key, count] : freqs) { + unique_counts.insert(count); + } + + return freqs.size() == unique_counts.size(); + } +};