import java.util.HashMap;
import java.util.Map;
public class MostOccuranceNumber {
public static void main(String[] args) {
int[] array1 = {1,2,4,1,3,6,1};
System.out.println(mostOccurance(array1));
}
public static Integer mostOccurance(int ...array) {
Integer mostCount=0;
Map<Integer,Integer> map = new HashMap<>();
Integer mostItem=null;
for(int i:array) {
if(map.containsKey(i)) {
int counter = map.get(i)+1;
map.put(i, counter);
}else {
map.put(i,1);
}
if(map.get(i) > mostCount) {
mostCount = map.get(i);
mostItem = i;
}
}
return mostItem;
}
}
Output:-
1
Wednesday, March 17, 2021
Most Frequently Occurring Item in an Array
Subscribe to:
Post Comments (Atom)
How to sort an HashMap based on value
import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.LinkedHashMap; import java....
-
http://www.roseindia.net/apachevelocity/velocity-tutorials.shtml
-
http://download.oracle.com/javase/tutorial/uiswing/start/index.html
-
function data_change(field) { var check = true; var value = field.value; //get characters //check that all ch...
No comments:
Post a Comment