-1

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.");
   }
}
Pioneer
  • 13
  • 2
  • 2
    Please always include all of the error messages you get when facing such problems. This line `int A[] = new int [];` will cause a compiler error, so you can't run your code. – jlordo Feb 20 '13 at 20:28
  • http://stackoverflow.com/questions/5287538/how-to-get-basic-user-input-for-java – happybuddha Feb 20 '13 at 20:28
  • I can understand what's your problem ? Are you trying to input for example 6 numbers to an array ? Explain .... – Azad Feb 20 '13 at 20:29

1 Answers1

1

How about this?

public static void main(String[] args) throws FileNotFoundException {
    int[] arrayWithMajorityElements = {3,3,4,2,4,4,2,4,4};

    Integer[] majorityElements = getMajorityElements(arrayWithMajorityElements);
    System.out.println(Arrays.toString(majorityElements));
    // result = 4

    int[] arrayWithoutMajorityElements = {3,3,4,2,4,4,2,4};
    majorityElements = getMajorityElements(arrayWithoutMajorityElements);
    System.out.println(Arrays.toString(majorityElements));
    // result = empty array (ie no majory elements)
}

private static Integer[] getMajorityElements(int[] array) {
    Map<Integer, Integer> map = new HashMap<>();
    for (int i : array) {
        int count = 0;
        if (map.containsKey(i)) {
            count = map.get(i);
        }
        map.put(i, ++count);
    }

    List<Integer> majorityElements = new ArrayList<>();
    int check = array.length / 2;
    for (Integer i : map.keySet()) {
        if (map.get(i) > check) {
            majorityElements.add(i);
        }
    }
    return majorityElements.toArray(new Integer[majorityElements.size()]);
}

Note that I've assumed that you can have more than one majority element - if you can't then you'd have to modify the last loop

Sean Landsman
  • 7,033
  • 2
  • 29
  • 33