Counting and comparing elements in an array

I have a method that randomly creates 0s, 1s, 2s to represent voters preferences in choosing a party. Now I need to create a second method that counts the 0s, 1s, 2s in the array to figure out the winner. Any ideas on how to do this?

Would something like this work?

public static int getwinner(int[] arr, int numToFind) {

int occurence=0;

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

if (arr[i] == numToFind)

occurence++;

return occurence;

Re: Counting and comparing elements in an array

Quote:

Originally Posted by

**TheBestGame**
Would something like this work?

What happened when you tried it?

Re: Counting and comparing elements in an array

Maybe you should think about this logically:

You need to count the amount of zeros, ones and twos in an array, and then find out which has the biggest count, correct?

Then I'll give you a push in the right direction: how about you make 3 counters, and for every occurrence of one of the numbers, you increment the corresponding counter by 1. After you've gone through the entire array, you test which number is the biggest, and there you go, counting made simple.

What I don't understand though, is what that parameter "int numToFind" has anything to do with whatever you're trying to do. Why are you searching for a specific number?