#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2012
    Posts
    1
    Rep Power
    0

    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;
  2. #2
  3. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,959
    Rep Power
    1014
    Hi,

    I don't understand what you expect from us. You already have an idea and a count method. If you want to know if it works, then test it.

    Apart from the fact that for some reason you've left out all closing braces and given the method the "wrong" name, yeah, it does work. It counts the occurences of a specific number.

    And now you need to compare these occurences. You could simply do this "by hand" by calculating the three values and comparing them one by one. Or you make a loop to get the maximum and the corresponding number.

IMN logo majestic logo threadwatch logo seochat tools logo