awd awd - 4 months ago 9
Java Question

how to use hashmap to find smallest mode in the array

The question is that given an array returns the most frequently occurring element of an array of positive integers. Assume that the array has at least one element .If there are more than one mode return the smallest mode.

Here is my code, but it can not pass this array {27, 15, 15, 11, 27}.

Also,how to modify my code without using

i
in for-each loop?

public int mode(int input[]){
    if(input.length==1)
        return input[0];
Map<Integer,Integer> mymap = new HashMap<Integer,Integer>();
    int count=0;
    for(int i = 0 ;i < input.length;i++){
        count = mymap.containsKey(input[i]) ? mymap.get(input[i]) : 0;
         mymap.put(input[i],count+1);
    }
    int firstmode = -1;
    int secondmode = -1;
    int i=0;
    for(int k :mymap.keySet()){
        if(mymap.get(k)>secondmode){
            i++;
      firstmode=k;
            if(i%2==0){ //if there more than one mode,then compare them
                if(firstmode>k){
                   firstmode=k;
                   i--;
                }
            }
        secondmode=mymap.get(k);
        }  
    }
    return firstmode;
}


Updated test cases

{1, 1, 2, 3} should return 1
{1, 1, 2, 3, 3} should return 1
{27, 15, 15, 11, 27} should return 15
{27, 15, 15, 11, 27, 27} should return 27
{1, 1, 6, 6, 6, 3, 3, 3} should return 3
{27, 15, 15, 27, 11, 11, 11, 14, 15, 15, 16, 19, 99, 100, 0, 27}
should return 15
{42} should return 42

Answer

I found your logic pretty unclear and convoluted so I just rewrote the code from scratch. I included a solution pre-Java 8 and another one post-Java 8.

import java.util.HashMap;
import java.util.Map;
import java.util.IntStream;

import static java.util.Comparator.naturalOrder;

public class Solution {
    public static int smallestMode(int input[]) {
        Map<Integer, Integer> counters = new HashMap<>();
        for (Integer i : input) {
            Integer counter = counters.get(i);
            counters.put(i, counter == null ? 1 : counter + 1);
        }

        int mode = Integer.MIN_VALUE;
        for (int counter : counters.values()) mode = Math.max(mode, counter);

        int result = Integer.MAX_VALUE;
        for (Map.Entry<Integer, Integer> entry : counters.entrySet()) {
            if (entry.getValue() == mode) result = Math.min(result, entry.getKey());
        }

        return result;
    }

    public static int smallestModeJava8(int input[]) {
        Map<Integer, Integer> counters =  IntStream.of(input).boxed().collect(toMap(k -> k, v -> 1, Integer::sum))
        int mode = counters.values().stream().max(naturalOrder()).get();
        return counters.entrySet().stream().filter(entry -> entry.getValue() == mode).map(Map.Entry::getKey).min(naturalOrder()).get();
    }

    public static void main(String[] args) {
        int[] input = {27, 15, 15, 11, 27};
        System.out.println(smallestMode(input));
        System.out.println(smallestModeJava8(input));
    }
}
Comments