Skip to content

Commit

Permalink
Merge pull request #1 from Shordore/Open-source-Contribution-1
Browse files Browse the repository at this point in the history
Frequency of an element in an array C++. Issue MakeContributions#1285.
  • Loading branch information
Shordore authored Feb 6, 2024
2 parents d3c2184 + 258c64e commit 0ad61ab
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions Frequency_Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include <iostream>
#include <vector>
#include <unordered_map>

std::unordered_map<int, int> FrequencyElement(const std::vector<int>& nums) {
std::unordered_map<int, int> un_map;

for (int i : nums) {
un_map[i]++;
}

return un_map;
}


int main() {
std::vector<int> inputArray = {1, 2, 3, 1, 2, 1, 4, 5, 4, 3};

std::unordered_map<int, int> result = FrequencyElement(inputArray);

for (const auto& pair : result) {
std::cout << "Element: " << pair.first << ", Frequency: " << pair.second << std::endl;
}

return 0;
}

0 comments on commit 0ad61ab

Please sign in to comment.