Open In App

How to find the Entry with largest Value in a C++ Map

Last Updated : 16 Jun, 2022
Comments
Improve
Suggest changes
Like Article
Like
Report

Given a map in C++, the task is to find the entry in this map with the highest value. Examples:

Input: Map = {ABC = 10, DEF = 30, XYZ = 20}
Output: DEF = 30

Input: Map = {1 = 40, 2 = 30, 3 = 60}
Output: 3 = 60

Approach

  1. Iterate the map entry by entry with help of iterators
map::iterator itr;
for (itr = some_map.begin();
     itr != some_map.end();
     ++itr) 
{
    // operations
}
  1. Store the first entry in a reference variable to compare to initially.
  2. If the current entry's value is greater than the reference entry's value, then store the current entry as the reference entry.
  3. Repeat this process for all the entries in the map.
  4. In the end, the reference variable has the required entry with the highest value in the map.
  5. Print this entry

Below is the implementation of the above approach: 

CPP
// 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; 
} 
Output:
Map: 1 = 40, 2 = 30, 3 = 60, 
Entry with highest value: 3 = 60

Time Complexity: O(nlogn)
Auxiliary Space: O(n)


Next Article
Article Tags :
Practice Tags :

Similar Reads