Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.lang.reflect.Array;
- import java.util.*;
- public class frequent {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- String input = scan.nextLine();
- input = input.replaceAll("\\s+", " ");
- String[] arr = input.split(" ");
- int[] numbers = new int[arr.length];
- for(int i =0;i<arr.length;i++){
- numbers[i] = Integer.parseInt(arr[i]);
- }
- System.out.println(mostFrequent(numbers,numbers.length));
- }
- static int mostFrequent(int arr[], int n)
- {
- Arrays.sort(arr);
- int max_count = 1, res = arr[0];
- int curr_count = 1;
- for (int i = 1; i < n; i++) {
- if (arr[i] == arr[i - 1]) {
- curr_count++;
- }else {
- if (curr_count > max_count){
- max_count = curr_count;
- res = arr[i - 1];
- }
- curr_count = 1;
- }
- }
- if (curr_count > max_count) {
- max_count = curr_count;
- res = arr[n - 1];
- }
- return res;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement