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

后端 未结 11 1626
后悔当初
后悔当初 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:21

    This is how i have implemented in java..

    import java.io.*;
    class Prog8
    {
        public static void main(String[] args) throws IOException 
        {
            BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
            System.out.println("Input Array Size:");
            int size=Integer.parseInt(br.readLine());
            int[] arr= new int[size];
            System.out.println("Input Elements in Array:");
            for(int i=0;i=max)
                {
                    max = count;
                    pos=i;
                }
            }
    
            if(max==1)
                System.out.println("No Duplicate Element.");
            else
                System.out.println("Element:"+arr[pos]+" Occourance:"+max);
        }
    }
    

提交回复
热议问题