Finding the Max Value in a Map

Finding the max value in a map

You never changed currentMax in your code.

map<int,unsigned> frequencyCount;
for(size_t i = 0; i < v.size(); ++i)
frequencyCount[v[i]]++;

unsigned currentMax = 0;
unsigned arg_max = 0;
for(auto it = frequencyCount.cbegin(); it != frequencyCount.cend(); ++it ) }
if (it ->second > currentMax) {
arg_max = it->first;
currentMax = it->second;
}
}
cout << "Value " << arg_max << " occurs " << currentMax << " times " << endl;

Another way to find the mode is to sort the vector and loop through it once, keeping track of the indices where the values change.

Find the Min of Max values in a MapString, ListInteger without relying on for loops

You can use the methods Collections.min and Collections.max

HashMap<String, List<Integer>> myMap = new HashMap<>();

Integer min = Collections.min(myMap.values().stream().map(Collections::max).collect(Collectors.toList()));

Finding Key associated with max Value in a Java Map

Basically you'd need to iterate over the map's entry set, remembering both the "currently known maximum" and the key associated with it. (Or just the entry containing both, of course.)

For example:

Map.Entry<Foo, Bar> maxEntry = null;

for (Map.Entry<Foo, Bar> entry : map.entrySet())
{
if (maxEntry == null || entry.getValue().compareTo(maxEntry.getValue()) > 0)
{
maxEntry = entry;
}
}

find element with max value from std::map

Taken from here:

auto x = std::max_element(m.begin(), m.end(),
[](const pair<int, int>& p1, const pair<int, int>& p2) {
return p1.second < p2.second; });

This, rather than using std::map::value_comp() (which compares the key values) looks at the second member in the pair, which contains the value. This uses a lambda expression, so you will have to compile with C++11 support

Get the highest values in a hashmap in java

The first step is to find the highest value at all.

int max = Collections.max(map.values());

Now iterate through all the entries of the map and add to the list keys associated with the highest value.

List<String> keys = new ArrayList<>();
for (Entry<String, Integer> entry : map.entrySet()) {
if (entry.getValue()==max) {
keys.add(entry.getKey());
}
}

If you like the Java 8 Stream API, try the following:

map.entrySet().stream()
.filter(entry -> entry.getValue() == max)
.map(entry -> entry.getKey())
.collect(Collectors.toList());

How to find max value of a map whose value is a List in Java8?

I just want to get the highest list size in the map

If I get correctly what you mean then you should try this:

int maxSize = studentsInClass.values()
.stream()
.map(List::size)
.max(naturalOrder())
.orElse(0);

how to find the key of max value of a map in flutter?

Map themap={"anger": 0.0, "contempt": 0.02, "disgust": 0.0, "fear": 2.0, "happiness": 0.0, "neutral": 0.978, "sadness": 0.002, "surprise": 0.0};

var thevalue=0.0;
var thekey;

themap.forEach((k,v){
if(v>thevalue) {
thevalue = v;
thekey = k;
}
});

print (thekey);

Javascript: Finding highest value in Map() vs Object

You can spread the values() into Math.max:

let m = new Map([['a', 2], ['b',4], ['c',6]])
console.log("Max:", Math.max(...m.values()))

How to find the Maximum value for specific key in ListTreeMapInteger,Integer?

You can flatten the entrySet of map using flatMap and collect as new map using Collectors.toMap with max value as the value of the key.

Map<Integer, Integer> res = 
listOfFreq.stream()
.flatMap(m -> m.entrySet().stream())
.collect(Collectors.toMap(e -> e.getKey(), e -> e.getValue(), Integer::max));

how to find the max value from all the value types in a map?

maxmap.end()->second <-- What your trying to do here is dereference the end iterator which is not valid.

Just use a for loop:

typedef my_map::iterator iter;
iter it = maxmap.begin();
iter end = maxmap.end();

float max_value = it->second;
std::string str = it->first;
for( ; it != end; ++it) {
if(*it->second > max_value) {
max_value = it->second;
str = it->first;
}
}


Related Topics



Leave a reply



Submit