-1

I have an array of integers and there are one or more elements that have the same integer value. I want to identify the majority integer value among the other integers. How is this accomplished.

4

3 Answers 3

2

I'm not sure if you are looking for an "optimum" solution however the code below works.

Method:

public static Integer majority(int[] array) {
    Map<Integer, Integer> count = new HashMap<Integer, Integer>();
    for (int number : array) {
        if (count.containsKey(number)) {
        count.put(number, count.get(number) + 1);
        } else {
        count.put(number, 1);
        }
    }
    Integer majority = null;
    Integer majorityCount = null;
    for (Integer key : count.keySet()) {
        if (count.get(key) > array.length / 2) {
        majority = key;
        majorityCount = count.get(key);
        }
    }
    return majority;
}

Here's the test runner:

public static void main(String[] args) {
    int[] array = {0, 1, 3, 4, 2, 1, 0, 0, 0, 10, 10, 0, 0};
    int[] array1 = {0, 1, 3, 4, 2, 1, 0, 0, 0, 0, 0};
    System.out.println(majority(array));
    System.out.println(majority(array1));
}

And the output

null
0

I hope this is what you are looking for.

1

Below code will tell you what the majority integer is and also how many times it appeared in the array. Obviously I haven't looked at optimizing the solution as it was not asked.

    int array[] = new int[10];

    array[0] = 0;
    array[1] = 9;
    array[2] = 1;
    array[3] = 2;
    array[4] = 3;
    array[5] = 6;
    array[6] = 7;
    array[7] = 8;
    array[8] = 5;
    array[9] = 4;

    HashMap map = new HashMap();

    int majorInt = 0;
    int maxCounter = 0;
    boolean majorIntFound = false;

    for (int i = 0; i < array.length; i++) {
        Integer counter = (Integer) map.get(array[i]);

        if (counter != null) {
            majorIntFound = true;
            int counterInt = counter.intValue();

            map.put(array[i], ++counterInt);

            if (counterInt > maxCounter) {
                maxCounter = counterInt;
                majorInt = array[i];
            }

        } else {
            map.put(array[i], new Integer(1));
        }

    }
    if (majorIntFound)
        System.out.println(" Majority  int is: " + majorInt + "  counter: " + maxCounter);
    else
        System.out.println("No majority Int");
0

I think you want something like this?

import java.util.Random;
public class Majority{
    public static void main(String [] args){

        int [] random = new int[100];
        Random r = new Random();
        for (int i : random){
            int j = r.nextInt(21);
            random[i] = j;
        }

        int majority = 0, currentCount = 0, highestCount = 0;
        for (int i : random){
            for (int j : random){
                if(random[i] == random[j])
                    currentCount++;
            }       
            if(currentCount > highestCount){
                majority = random[i];
                highestCount = currentCount;
            }
        }
        System.out.print("The majority number is: " + majority);
    }
}
1
  • My code counts the number that occurs most, not the majority element. The OP did not define majority so I was mistaken about what it was, which is, I think, an element that occurs more than half the time. Feb 21, 2013 at 17:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.