Question:
java code to find the most occuring integer in an array.?
asd a
2008-11-04 12:17:32 UTC
I need help writing a loop to find the most occuring integer stored in an array. I have made a loop for the user to enter in integers to the amount they want and the integers are between 0 and 100. I just need to know a loop to find the most occuring integer in the array and display that. thanks
Three answers:
conventional
2008-11-04 12:49:49 UTC
HashMap hash = new HashMap();

public void store(int userInt){

hash.put(userInt, (hash.get(userInt) == null ? "1" :

Integer.parseInt(hash.get(userInt).split(":")[0]) + 1) + ":" + userInt);

}



public void getMode() {

String[] ans = hash.values().iterator().next().split(":");

System.out.println("The mode is " + ans[1] + "" +

". It was entered " + ans[0] + " times.");

}
connoisseur
2008-11-04 12:28:50 UTC
first sort the numbers in ascending order.

then put a loop and count the no of same values u get. make that the max value and make that no the most rec number.

loop till all the numbers are checked.

then the max value will be the most recurring number
coulanges
2016-11-08 14:15:36 UTC
Wikipedia has a reliable internet site with a table itemizing the great O of many different sorting algorithms. i could bypass with quicksort with the aid of fact it is speedy and actual recognizable, human beings'd be all like "omg he can code quicksort" :)


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...