Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Counting and comparing elements in an array

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Talking 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
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Counting and comparing elements in an array

    Quote Originally Posted by TheBestGame View Post
    Would something like this work?
    What happened when you tried it?
    Improving the world one idiot at a time!

  3. #3
    Junior Member
    Join Date
    Oct 2012
    Posts
    3
    My Mood
    Fine
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default 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?

Similar Threads

  1. Comparing Input to array
    By hbonh in forum What's Wrong With My Code?
    Replies: 5
    Last Post: November 28th, 2011, 10:43 AM
  2. Comparing similar elements in an array
    By FJIW in forum Algorithms & Recursion
    Replies: 2
    Last Post: September 25th, 2011, 10:22 AM
  3. Comparing elements of an arrayList with an array?
    By DudeJericho in forum Collections and Generics
    Replies: 2
    Last Post: April 21st, 2011, 12:39 PM
  4. URGENT!!!! help with array element comparing
    By Neo in forum Java Theory & Questions
    Replies: 3
    Last Post: March 3rd, 2011, 08:52 AM
  5. Help with Arrays - Counting elements
    By ShakeyJakey in forum Collections and Generics
    Replies: 7
    Last Post: August 8th, 2010, 04:09 PM