-1

I have an array:

int[] anArray = new int[6];

What if the array contained 1,4,5,4,4? How can I get the most matches? In this case 4 is the most frequent number, and there are three of them, so the function should return 3.

Or if I have 1,2,1,2,3 it should return 2. Or if I have 4,0,1,2,3 it should return 1.

I really can't figure it out. I tried this:

public static int getCorrectMatches(int[] flowers) throws Exception {
    int highest = 0;
    for(int index = 0; index < 5; index++) {
        int count = countMatches(flowers, index);
        if(count > highest) {
            highest = index;
        }
    }
    return highest;
}

public static int countMatches(int[] array, int match) throws Exception {
    int count = 0;
    for(; count < array.length && array[count] == match; count++);
    return count;
}

Which didn't work. I'd appreciate any help.

Darshan Rivka Whittle
  • 32,989
  • 7
  • 91
  • 109
  • Your `countMatches` function is wrong. You should test it and do some debugging. Oh and you probably meant `highest = count` instead of `highest = index` – Niklas B. Apr 30 '14 at 23:44
  • `array[count] == match` this is suspicious and `throws Exception` is not needed – nachokk Apr 30 '14 at 23:45
  • 1
    Why should the last case (4,0,1,2,3) return 1? – cyber-monk Apr 30 '14 at 23:49
  • 1
    @user3591226 instead of editing my answer, post a comment and I'll respond to the comment – cyber-monk Apr 30 '14 at 23:52
  • 1
    possible duplicate of [Java - Find the most popular element in int\[\] array](http://stackoverflow.com/questions/8545590/java-find-the-most-popular-element-in-int-array) among *numerous* others – Brian Roach May 01 '14 at 00:01

4 Answers4

3

Iterate over the array and for each number store a counter in a hashmap where the key of the map is the number and the value is the counter that tracks the number of occurrences. The first time you encounter a new number (for which there is no key) you'll need insert a new counter. The next time you encounter that same number you simply update the existing counter with a new number.

It also wouldn't be too hard to keep a running 'most matches' number and update it each time you are updating a counter.

cyber-monk
  • 5,470
  • 6
  • 33
  • 42
0
for each index in array
HashMap.put(number on index i, occurrences) +=1
check biggest value in the hash map

http://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html#put(K,%20V)

Jakkra
  • 641
  • 8
  • 25
0
public static int numberOfMatches(int[] numbers) {
    int mostMatches = 0;

    for(int i = 0; i < numbers.length; i++) {
        int matches = 0;
        int holder = numbers[i];

        for(int number : numbers) {
            if(number == holder) {
                matches++;
            }
        }

        if(matches > mostMatches)
            mostMatches = matches;
    }
    return mostMatches;
}

This accepts an array. It checks the length of the array, and loops for each slot. For each slot, I create 2 variables. holder grabs the value in the slot, and we increase matches any time there is a match.

Once all possible matches have been found for that slot, it compares the amount of matches found to the highest amount of matches found so far.

Vince
  • 14,470
  • 7
  • 39
  • 84
0

Your countMatches() method is incorrect. Decouple count from the loop variable and check the equality inside the loop, instead of putting it in the loop condition. Change it to something like this:

public static int countMatches(int[] array, int match) throws Exception {
    int count = 0;

    for(int i=0; i< array.length ; i++){

        if(array[i] == match) count++;
    }

    return count;
}

Also, if I were you, I'd change

for(int index = 0; index < 5; index++) {

to,

for(int index = 0; index < flowers.length; index++) {
Andrew Lobley
  • 342
  • 4
  • 12