Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

maximum occuring element in java

public class Main {
    public static void main(String[] args) {
        //intial array
        int arr[] = {2, 4, 6, 4, 2, 4, 5, 8};
        int max_element = 8;
        
        //declare an array of size max_element+1
        int count_arr[] = new int[max_element+1];
        
        //loop through the original array and update the count
        for(int i=0; i<arr.length; i++){
            count_arr[arr[i]]++;
        }
        
        //fetch the element having the max count
        int max_repeated = Integer.MIN_VALUE;
        int max_count = -1;
        for(int i=0; i<arr.length; i++){
            if(count_arr[arr[i]] > max_count){
                max_count = count_arr[arr[i]];
                max_repeated = arr[i];
            }
        }
        
        System.out.println("Most repeated: "+max_repeated); 
    }
}
Comment

PREVIOUS NEXT
Code Example
Java :: get material of block bukkit 
Java :: import claim jwt 
Java :: reponse entity as result spring controller 
Java :: all loops in java 
Java :: spring security specific url for specific account 
Java :: swagger apiimplicitparam all endpoints 
Java :: access modifier overloaded method 
Java :: how to do 4th root java 
Java :: how to easy get 240 fps in minecraft java 
Java :: What is sleep() method 
Java :: java swing place objects vertically 
Java :: crud repository count number of items in a list 
Java :: what is abstract class 
Java :: nullpointerexception 
Java :: java classes and objects 
Java :: Build path specifies execution environment JavaSE-1.8. There are no JREs installed in the workspace that are strictly compatible with this environment. 
Java :: Java if...else...if Statement 
Java :: java while loop 
Java :: java final method 
Java :: nested for loop java 
Java :: can you use java in unity 
Java :: Black belt in grepper 
Java :: java random value threadlocalrandom 
Java :: how to get history stack in android webview 
Sql :: sql server drop temp table if exists 
Sql :: mysql current running queries 
Sql :: sql ignore accents 
Sql :: oracle show grants on table 
Sql :: mysql query to get column names 
Sql :: mysql grant all privileges to user from any host 
ADD CONTENT
Topic
Content
Source link
Name
4+1 =