-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.

millimoose
  • 39,073
  • 9
  • 82
  • 134
Sathirar
  • 331
  • 4
  • 17
  • 3
    Define majority integer number. Is it a number found more times than any other? Or maybe an integer number found more times than **all the others taken together** – Ivaylo Strandjev Feb 21 '13 at 16:12
  • possible duplicate of [Linear time majority algorithm?](http://stackoverflow.com/questions/4280450/linear-time-majority-algorithm) – amit Feb 21 '13 at 16:14
  • 1
    Also, this answer adresses the exact same thing: http://stackoverflow.com/a/9487018/151344 – Alderath Feb 21 '13 at 16:20
  • possible duplicate of [Find majority element in array](http://stackoverflow.com/questions/4325200/find-majority-element-in-array) – Yusubov Feb 21 '13 at 16:57

3 Answers3

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.

nattyddubbs
  • 2,085
  • 15
  • 24
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");
user_CC
  • 4,686
  • 3
  • 20
  • 15
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);
    }
}
ChrisWilson4
  • 195
  • 4
  • 23
  • 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. – ChrisWilson4 Feb 21 '13 at 17:07