Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class MostCommonElement {
- public static void GetMostCommonNumber(int[] arr){
- int count = Integer.MIN_VALUE;
- int tempCount = 0;
- int MostCommon = 0;
- int temp;
- for (int i = 0; i < arr.length - 1; i++){
- temp = arr[i];
- tempCount = 0;
- for (int j = 1; j < arr.length; j++){
- if (temp == arr[j]) {
- tempCount++;
- }
- }
- if(tempCount > count){
- MostCommon = temp;
- count = tempCount;
- }
- }
- System.out.println("The most common number: " + MostCommon + " --> " + tempCount + " times");
- }
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter numbers: ");
- String text = scan.nextLine();
- String[] str = text.split(" ");
- int[] arr2 = new int[str.length];
- for (int i = 0; i < arr2.length; i++) {
- arr2[i] = Integer.parseInt(str[i]);
- }
- GetMostCommonNumber(arr2);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement