I tried different ways to get sequence of input from the user but don't know what to do. Here is my code. A majority element in an array A of size N is an element that appears more than N/2 times. For example (3,3,4,2,4,4,2,4,4) has a majority element (4), whereas the array (3,3,4,2,4,4,2,4) does not have majority element. I am trying to get sequence of input from the user.
import java.util.Scanner;
class rough1 {
public static int arrMajority1( int A[] ) {
int n = A.length;
for( int i = 0; i < A.length; i++ ) {
int c = 1;
for( int j = i + 1; j < A.length; j++ )
if( A[ i ] == A[ j ] )
c = c + 1;
if( c > ( A.length / 2 )) {
return A[ i ];
}
}
return -1;
}
public static void main(String[] args){
Scanner input = new Scanner(System.in);
int A[] = new int [];
A[] = input.nextInt();
String employee = "A[]";
String delims = "[,]";
String[] tokens = employee.split(delims);
if (arrMajority1(A) != -1)
System.out.println("The majority element is " + arrMajority1(A));
else
System.out.println("There is no majority element.");
}
}