Most Frequent

QUESTION

This is one of the most easiest problem you will ever code.\n\nMr. Bournville loves programming and he likes to face new programming challenges. After completing many challenges he has now given you one challenge which is one of his favourites. He has given you a list of N random integers and he wants you to find the integer which has the maximum frequency in the given list.\n\nBeing a programmer himself, he had made this task a challenge for you and he will calculate your score for this task using a formula.\n\nFormula for calculating the score : (max_score) – (number of characters in your code/15.0)\n\nMr. Bournville already has a solution for this but he is not satisfied with his solution. He wants you to write a shortest possible code for this task. In case Frequency of two numbers is same print the smaller one\n\nInput:\nFirst line of input contains N, number of integers.\nSecond line will contains N spaces separated integers.\n\nOutput:\nPrint the most frequent integer.\n\nContraints:\n3<=N<=10^4\n-10^6<=Integer<=10^6.

“TESTCASE_1”: “5\n1 1 1 2 2\n###—###SEPERATOR—###—\n1”, “TESTCASE_2”: “7\n-2 -2 -2 2 2 2 1\n###—###SEPERATOR—###—\n-2”, “TESTCASE_3”: “50\n0 0 1 0 0 1 1 1 1 0 1 1 1 0 0 1 1 1 1 0 1 0 1 0 0 0 0 0 1 1 1 1 0 1 1 1 1 0 0 1 0 1 1 0 1 1 0 0 1 1\n###—###SEPERATOR—###—\n1”, “TESTCASE_4”: “7\n2 2 2 4 4 1 1\n###—###SEPERATOR—###—\n2”, “TESTCASE_5”: “0\n###—###SEPERATOR—###—\n0

ANSWER

import java.io.*;
import java.util.*;
// Warning: Printing unwanted or ill-formatted data to output will cause the test cases to fail
 
class TestClass {
    public static void main(String args[] ) throws Exception {
        Scanner sc= new Scanner(System.in);
        int len=sc.nextInt();
        int arr[]= new int[len];
        for(int i=0;i<len;i++){
            arr[i]=sc.nextInt();
        }
       // Arrays.sort(arr);
        int max=0,index=0;
        for(int i=0;i<len;i++){
            int count=0;
            for(int j=0;j<len;j++){
                if(arr[i]==arr[j])
                count++;
            }
            if(count>max){
                max=count;
                index=i;
            }
           
        }
         System.out.print(arr[index]);
    }
}
Ads Blocker Image Powered by Code Help Pro

Ads Blocker Detected!!!

We have detected that you are using extensions to block ads. Please support us by disabling these ads blocker.

Powered By
CHP Adblock Detector Plugin | Codehelppro