fork download
  1. import java.util.HashMap;
  2. import java.util.Map;
  3.  
  4. /* The class name doesn't have to be Main, as long as the class is not public. */
  5. class Main
  6. {
  7. public static void main (String[] args) throws java.lang.Exception
  8. {
  9. Map<Integer, Integer> maxMap = new HashMap<Integer, Integer>();
  10. int[][] pairs = {
  11. { 1, 2 },
  12. { 1, 4 },
  13. { 1, 3 },
  14. { 2, 2 },
  15. { 2, 5 }
  16. };
  17. // Calculate max value for each itemid
  18. for (int i = 0; i < pairs.length; i++) {
  19. int[] pair = pairs[i];
  20. Integer currentMax = maxMap.get(pair[0]);
  21. if (currentMax == null) {
  22. currentMax = Integer.MIN_VALUE;
  23. }
  24. maxMap.put(pair[0], Math.max(pair[1], currentMax));
  25. }
  26. // Print them
  27. for (Integer itemId : maxMap.keySet()) {
  28. System.out.printf("%d %d\n", itemId, maxMap.get(itemId));
  29. }
  30. }
  31. }
Success #stdin #stdout 0.03s 245632KB
stdin
Standard input is empty
stdout
1 4
2 5