Advertisement
sedran

Exercise 11.15: maxOccurrences : Practice-It

Apr 11th, 2011
262
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | None | 0 0
  1. public static int maxOccurrences(List<Integer> list) {
  2.    if(list.isEmpty()) {
  3.       return 0;
  4.    } else {
  5.       Map<Integer, Integer> m = new HashMap<Integer,Integer>();
  6.       Iterator<Integer> it = list.iterator();
  7.       int max = 0;
  8.       while( it.hasNext() ) {
  9.          int next = it.next();
  10.          int val = 0;
  11.          if( m.containsKey(next) ) {
  12.             int value = m.get(next)+1;
  13.             m.put(next, value);
  14.             val = value;
  15.          } else {
  16.             m.put(next, 1);
  17.             val = 1;
  18.          }
  19.          if( val>max ) {
  20.             max = val;
  21.          }
  22.       }
  23.       return max;
  24.    }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement