So I got this problem in my homework, which asks me to implement with both set and map respectively to solve for the numbers of modes in an array(to find the data that has the highest frequency of occurrence within a set of data). It also requires to test and show solutions for no modes, 1 mode, and multiple modes, These are literally all the instructions I have(The teacher isn't keen on being specific). As far as I understand, neither set or map allows for duplicates values, then how would there even exist modes? I am very iffy. Can someone give me some insights on this one, should it be doable? I would appreciate any pointers to kick this off. Thanks!!

1 Answers

user3586940 On

The solution is really simple, you just have a map that keeps count of how many times each integer showed up and the iterate through the map to find the key with the largest count. The solution is the last one right here:

As for the solution with the set, your probably best of using a ordered multiset that will keep things in the right order and then just iterate with a variable max count and current count. Replace most_freq (another variable) with the current element when current count is > than max count. That is basically the solution. I could code this up in 3 minutes, but I want you to do your homework.