Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,959 questions

51,901 answers

573 users

How to find elements that appear more than array_size/K times in an array with C++

1 Answer

0 votes
#include <iostream>
#include <unordered_map>

std::unordered_map<int,int> elements_that_appear_more_than_x_times(int arr[], int k, int size) {
    int times = size / k;
    std::cout << "more than " << times << " times" << "\n";
    
    std::unordered_map<int, int> freqency_map;
    
    for (int i = 0; i < size; i++) {
        freqency_map[arr[i]]++;
    }
    
    return freqency_map;
}

int main()
{
    int k = 4;
    int arr[] = {4, 8, 6, 5, 5, 8, 3, 2, 1, 2, 2, 5, 5, 5, 5, 8, 9, 8, 8};
    int size = sizeof(arr) / sizeof(arr[0]);
    
    std::unordered_map freqency_map = elements_that_appear_more_than_x_times(arr, k, size);

    for (auto el: freqency_map) {
        if (el.second > size / k) {
            std::cout << el.first << "\n";
        }
    }
}




/*
run:

more than 4 times
5
8

*/

 



answered Feb 10, 2024 by avibootz
edited Feb 10, 2024 by avibootz
...