Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

need to take sequence of user input

$
0
0
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.Arrays;
import java.util.Scanner;

public class rough1{
public static int arrMajority1(int A[]){
Scanner s = new Scanner(System.in);

int count = s.nextInt();
s.nextLine(); // throw away the newline.

int [] numbers = new int[count];
Scanner numScanner = new Scanner(s.nextLine());
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){
int A[] = new int [] {1,1,7,5};
// int arrMajority1 = A[0];
if (arrMajority1(A) != -1)

System.out.println("The majority element is " + arrMajority1(A));

else
System.out.println("There is no majority element.");
}
}

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>