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

• Difficulty Level : Basic
• Last Updated : 17 Mar, 2020

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
```

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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
}
```
2. Store the first entry in a reference variable to compare to initially.
3. If the current entry’s value is greater than the reference entry’s value, then store the current entry as the reference entry.
4. Repeat this process for all the entries in the map.
5. In the end, the reference variable has the required entry with the highest value in the map.
6. Print this entry

Below is the implementation of the above approach:

 `// C++ program to find the Entry``// with largest Value in a Map`` ` `#include ``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
```

My Personal Notes arrow_drop_up