Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
665 B
3
Indexable
Never
#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

int findMostFrequentElement(const vector<int>& arr) {
    unordered_map<int, int> frequencyMap;
    int maxCount = 0, mostFrequentElement = -1;

    for (int num : arr) {
        frequencyMap[num]++;
        if (frequencyMap[num] > maxCount) {
            maxCount = frequencyMap[num];
            mostFrequentElement = num;
        }
    }

    return mostFrequentElement;
}

int main() {
    vector<int> arr = {1, 3, 2, 1, 4, 1, 3, 4, 4, 4};
    cout << "The element which appears maximum number of times is: " 
         << findMostFrequentElement(arr) << endl;
    return 0;
}
Leave a Comment