Count Number Of Occurrence in an Array : Using a map it can be done, key will become number of array and value will become its occurrence, each time need to check in map if key is present so get result +1, else add 1 to its value.
public class CounterNumbers {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> list = Arrays.asList(3,2,3,4,5,6,2,2,4);
for(int i=0;i<list.size();i++) {
if(map.containsKey(list.get(i))) {
map.put(list.get(i), map.get(list.get(i))+1);
}else {
map.put(list.get(i), 1);
}
}
System.out.println(map);
}
}
Result : {2=3, 3=2, 4=2, 5=1, 6=1}
public class CounterNumbers {
public static void main(String[] args) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
List<Integer> list = Arrays.asList(3,2,3,4,5,6,2,2,4);
for(int i=0;i<list.size();i++) {
if(map.containsKey(list.get(i))) {
map.put(list.get(i), map.get(list.get(i))+1);
}else {
map.put(list.get(i), 1);
}
}
System.out.println(map);
}
}
Result : {2=3, 3=2, 4=2, 5=1, 6=1}