Search
 
SCRIPT & CODE EXAMPLE
 

CPP

maximum value in map in c++

#include <bits/stdc++.h>
using namespace std;

bool compare(const pair<int, int>&a, const pair<int, int>&b)
{
   return a.second<b.second;
}

int main(int argc, char const *argv[])
{
   int n, key, maxn;
   map<int,int> mp;

   cin>>n;

   for (int i=0; i<n; i++)
   {
     cin>>key;
     mp[key]++;
   }

   maxn = max_element(mp.begin(), mp.end(), compare)->second;

   cout<<maxn<<endl;

   return 0;
 }
Comment

maximum value in map in c++

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

Maximum element in a map c++

// C++ program to find the Entry
// with largest Value in a Map
  
#include <bits/stdc++.h>
using namespace std;
  
// Function to print the Map
void printMap(map<int, int> sampleMap)
{
    map<int, int>::iterator itr;
    for (itr = sampleMap.begin();
         itr != sampleMap.end();
         ++itr) {
        cout << itr->first
             << " = " << itr->second << ", ";
    }
    cout << endl;
}
  
// Function tp find the Entry
// with largest Value in a Map
pair<int, int> findEntryWithLargestValue(
    map<int, int> sampleMap)
{
  
    // Reference variable to help find
    // the entry with the highest value
    pair<int, int> entryWithMaxValue
        = make_pair(0, 0);
  
    // Iterate in the map to find the required entry
    map<int, int>::iterator currentEntry;
    for (currentEntry = sampleMap.begin();
         currentEntry != sampleMap.end();
         ++currentEntry) {
  
        // If this entry's value is more
        // than the max value
        // Set this entry as the max
        if (currentEntry->second
            > entryWithMaxValue.second) {
  
            entryWithMaxValue
                = make_pair(
                    currentEntry->first,
                    currentEntry->second);
        }
    }
  
    return entryWithMaxValue;
}
  
// Driver code
int main()
{
  
    // Map
    map<int, int> sampleMap;
    sampleMap.insert(pair<int, int>(1, 40));
    sampleMap.insert(pair<int, int>(2, 30));
    sampleMap.insert(pair<int, int>(3, 60));
  
    // Printing map
    cout << "Map: ";
    printMap(sampleMap);
  
    // Get the entry with largest value
    pair<int, int> entryWithMaxValue
        = findEntryWithLargestValue(sampleMap);
  
    // Print the entry
    cout << "Entry with highest value: "
         << entryWithMaxValue.first << " = "
         << entryWithMaxValue.second << endl;
  
    return 0;
}
Comment

PREVIOUS NEXT
Code Example
Cpp :: loop through char in string c++ 
Cpp :: structure and function c++ 
Cpp :: cpp merge two sets 
Cpp :: how to declare 1-D array in C/C++ 
Cpp :: array 2d dynamic allocation c++ 
Cpp :: in c++ ++ how to write if without if 
Cpp :: c++ check first character of string 
Cpp :: c++ code for selection sort 
Cpp :: how to add numbers in c++ 
Cpp :: include spaces while reading strings in cpp 
Cpp :: remove last character from string c++ 
Cpp :: c++ program to take input from user 
Cpp :: maximum int c++ 
Cpp :: max function in c++ 
Cpp :: array and for loop in c++ 
Cpp :: change to lowercase character c++ 
Cpp :: iterate over map c++17 
Cpp :: c++ nagetive to positive numbers 
Cpp :: error: Microsoft Visual C++ 14.0 or greater is required. Get it with "Microsoft C++ Build Tools": https://visualstudio.microsoft.com/visual-cpp-build-tools/ 
Cpp :: print each number of digit c++ 
Cpp :: check if character is uppercase c++ 
Cpp :: What is the story of c++ 
Cpp :: how to add an element to std::map 
Cpp :: c++ iterate over vector of pointers 
Cpp :: c++ remove last character from string 
Cpp :: console colors in C++ 
Cpp :: c++ power 
Cpp :: c++ cast to type of variable 
Cpp :: How to create files in C++ 
Cpp :: factorial loop c++ 
ADD CONTENT
Topic
Content
Source link
Name
1+4 =