How to efficiently find the Lowest Key of the Highest Value in a map with a constraint that it can contain duplicate Values?

Sanjay Pradeep

My doubt was raised because of this particular question:

https://www.hackerrank.com/challenges/migratory-birds/problem?h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen&h_r=next-challenge&h_v=zen

I know that it can be easily solved by:

  1. Store the frequency in Map
  2. Initialize two temp variables a,b
  3. Start a loop

     - Store the current max value & key to a,b
     - If we find similar value: store the lowest key pair to b.
    
  4. Return b value

* But I don't want to do this. I want to solve the problem with Map using Streams *

Input arr contains 11 values, 1 2 2 2 3 4 5 5 4 3 4

Output should be 2

Explanation 1

The different types of birds occur in the following frequencies:

  • Type 1: 1
  • Type 2: 3
  • Type 3: 2
  • Type 4: 3
  • Type 5: 2

Two types have a frequency of 3, and the lower of those is type 2

I tried the below code which actually worked and I don't know how. I used a max function expecting that i will get an error because i have two Values with max value 3 from my example.

But Take a look at the migratoryBirds() function where I used Collections.max , the query returned me the map entry with lowest key of the highest value.

import java.io.*;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

public class Solution{

    // Complete the migratoryBirds function below.
    static int migratoryBirds(List<Integer> arr) {
        Map<Integer,Integer> map = new HashMap<>();
        int temp=0;
        for(int val : arr){
            if(map.containsKey(val)){
                temp=map.get(val);
                map.put(val,temp+1);
            }else{
                map.put(val,1);
            }
        }
        Map.Entry<Integer,Integer> maxEntry = Collections.max(map.entrySet(),
                (e1, e2) -> e1.getValue().compareTo(e2.getValue()));
        return maxEntry.getKey();
    }

    public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

        int arrCount = Integer.parseInt(bufferedReader.readLine().trim());

        List<Integer> arr = Stream.of(bufferedReader.readLine().replaceAll("\\s+$", "").split(" "))
                .map(Integer::parseInt)
                .collect(toList());

        int result = migratoryBirds(arr);

        bufferedWriter.write(String.valueOf(result));
        bufferedWriter.newLine();

        bufferedReader.close();
        bufferedWriter.close();
    }
}

Can anyone explain me why i got the expected result and suggest a proper alternate way for it using Map Streams.

Sorry if my question is weird, I love to look at all possible ways.

Naman

What you can possibly make use of is :

int minKeyWithMaxValueEntry = map.entrySet()
        .stream()
        .collect(Collectors.toMap(Map.Entry::getValue, Map.Entry::getKey, 
                Integer::min, TreeMap::new))
        .lastEntry()
        .getValue();

to elaborate the collect operation:

  • It stores the values from your initial Map entries as keys, correspondingly for each key in your entry is transformed into a value of resultant Map.

  • Since there could be multiple keys with the same values, the merge function Integer::min chooses the minimum among them.

  • All of this is stored in TreeMap to ensure that the resultant Map is sorted comparing its keys.

  • Out of this the last entry (for maximum value in the initial Map) is selected and the value from the final Map is stored as the output.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Find the lowest / highest key of a distributed map

how to get the highest dictionary key with the lowest value

How to find highest and lowest value on substrings?

How to find the lowest and highest date based on certain value in another column?

How to find highest and lowest value and aggregate into a string in Pandas, Pysimplegui

How to I find lowest and highest value of this array (java)

How can I find highest and lowest in an array of objects/ Javascript

Find Nth highest/lowest values in Pine

Java sort HashMap by highest value, then lowest key

How Can I get the Map key by a certain value? E.g. Map.prototype.get -> key by the lowest value

How can i find the highest and the lowest value between rows depending on a condition being met in another column in a pd.DataFrame?

Find the highest and lowest value for a time frame

how to find the key with the highest value in a dict of dicts?

How to find the highest and lowest number with JS?

How to find the highest and lowest product price

How to get the lowest and highest values for interval class

How to cut off 5% of the highest and lowest values

How to sort dictionary values from Highest to lowest?

Give multiple dictionaries can I get the lowest(or highest)value for each key?

How to rearrange from lowest to highest value in Python?

FIND THE HIGHEST AND LOWEST SALARY

How can I get the rows with the most cells that have the highest (or the lowest) values

How can I arrange a dictionary using python so that the values are represented from lowest to highest order?

How can I print the value of the lowest key of an array?

Find 10% highest and lowest values, trim all columns in R

JS: Sort array of objects highest to lowest by key value of the object

How to select the key corresponding to highest value from histogram map?

How can i reorder table rows based on the highest to lowest value found in each td cell?

How can I sort a money (I used VARCHAR) from lowest value to highest in T-SQL?