Find the element with highest occurrences in an array [java]

后端 未结 11 1496
后悔当初
后悔当初 2021-01-18 02:36

I have to find the element with highest occurrences in a double array. I did it like this:

int max = 0;
for (int i = 0; i < array.length; i++) {
       in         


        
11条回答
  •  迷失自我
    2021-01-18 03:35

    I will suggest another method. I don't know if this would work faster or not.

    Quick sort the array. Use the built in Arrays.sort() method.

    Now compare the adjacent elements. Consider this example:

    1 1 1 1 4 4 4 4 4 4 4 4 4 4 4 4 9 9 9 10 10 10 29 29 29 29 29 29

    When the adjacent elements are not equal, you can stop counting that element.

提交回复
热议问题