Skip to content
This repository has been archived by the owner on May 29, 2024. It is now read-only.

Frequency of each element in an array #1304

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions algorithms/CPlusPlus/Arrays/frequency-of-number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <iostream>
#include <unordered_map>
#include <vector>

void countFrequency(const std::vector<int>& array) {
std::unordered_map<int, int> frequencyMap;

for (int element : array) {
frequencyMap[element]++;
}

for (const auto& pair : frequencyMap) {
std::cout << "Element " << pair.first << ": " << pair.second << " times\n";
}
}

int main() {
int size;
std::cout << "Enter the size of the array: ";
std::cin >> size;

std::vector<int> array(size);
std::cout << "Enter the elements of the array:\n";
for (int i = 0; i < size; ++i) {
std::cout << "Element " << i + 1 << ": ";
std::cin >> array[i];
}

countFrequency(array);

return 0;
}