Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public int rarest(Map<String, Integer> m) throws Exception {
- Map<Integer,Integer> temp = new HashMap<Integer,Integer>();
- if(m.isEmpty()) {
- throw new Exception();
- } else {
- Iterator<Integer> it = m.values().iterator();
- while( it.hasNext() ) {
- int key = it.next();
- if( temp.containsKey(key) ) {
- int value = temp.get(key)+1;
- temp.put(key,value);
- } else {
- temp.put(key,1);
- }
- }
- it = temp.keySet().iterator();
- int min = it.next();
- int minCount = temp.get(min);
- while(it.hasNext()) {
- int next = it.next();
- int val = temp.get(next);
- if( val < minCount ) {
- minCount = val;
- min = next;
- } else if( val==minCount && next < min) {
- minCount = val;
- min = next;
- }
- }
- return min;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement