简体   繁体   中英

Finding frequency of most frequent element inside an array

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.

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.

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)

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.

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++) {

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM