From 3baf7d792679bc9e44e4995ba7d24c40e1a27330 Mon Sep 17 00:00:00 2001 From: Mayank Agrawal Date: Tue, 4 Oct 2022 14:55:48 +0530 Subject: [PATCH] Counting frquencies using map --- Hashing/countingfrequencies.cpp | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 Hashing/countingfrequencies.cpp diff --git a/Hashing/countingfrequencies.cpp b/Hashing/countingfrequencies.cpp new file mode 100644 index 00000000..f59205d5 --- /dev/null +++ b/Hashing/countingfrequencies.cpp @@ -0,0 +1,28 @@ +#include +using namespace std; + +void CountFreq(int arr[], int n) // function to count the frequency of the element +{ + unordered_map h; + for (int i = 0; i < n; i++) + { + h[arr[i]]++; + } + for (auto e : h) + { + cout << e.first << " " << e.second << endl; + } +} + +int main() +{ + int n; // size of the array + cin >> n; + int arr[n]; + for (int i = 0; i < n; i++) + { + cin >> arr[i]; + } + CountFreq(arr, n); + return 0; +} \ No newline at end of file