You can do it using Java 8's streams, e.g.:
String number = "2 4 3 4 2 4 0";
String[] array = number.split(" ");
TreeMap<Integer,Long> numberMap = Arrays.stream(array)
.map(s -> Integer.parseInt(s))
.collect(Collectors.groupingBy(Function.identity(), TreeMap::new, Collectors.counting()));
System.out.println(numberMap.descendingMap().firstEntry().getValue());
This basically stores each number and it's count into a Map
. As the Map
we are using is TreeMap
, it sorts the keys in ascending order. We then get the last (i.e. highest) key from it and print the corresponding value which is 3.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…