Results 1 to 3 of 3
  1. #1
    rachem is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Count occurances in array

    Hello, I am new to java and am trying to count the highest occruance that appears in an array but I'm not entirely sure how to do this

    so far I've got

    public class counter {
    public static void main(String[] args) {
    final int N = (args.length);
    int count= 1;
    int[] a;
    a = new int[N];
    for (int i = 0; i < N; i++)
    a[i] = Integer.parseInt(args[i]);
    for (int i = 0; i < N; i++)
    for(int j = i+1; j<N; j++)
    if (a[i] == a[j]) {
    count++;
    }

    System.out.println(duplicate);
    }
    }

    But this just increments the count by one everytime ANY duplicate is found, I have no idea how to pick the highest occurance of a number out

    Any help would be appreciated

    Rachel

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,759
    Rep Power
    7

    Default Re: Count occurances in array

    You will need three variables:
    a: the current count
    b: the highest count so far
    c: the value that has the highest count so far.

    Then use 2 nested loops. Count how many times the first number occurs using a. Store the count in b and the number in c. Reset a to 0 and go around again counting the second number. If the count for the second number is higher than b then reassign b and c. etc.

    Note: use better variable names than a b and c.

  3. #3
    rachem is offline Member
    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Default Re: Count occurances in array

    Can someone just confirm that this is right to count and store the number?

    Java Code:
    if (a[i] == a[j]) {
    	value = a[i];
    	if (value == a[j]) {
    	count++;	
    	duplicate = count;
    	  }
    	}
    	count = 1;
    Thank you, I think I'm just not getting something here :(

    EDIT: Ok for some reason it's counting everything past the second occurance as that number
    eg. if I type in 3 4 3 5 6 it'll give me a count of 4 instead of 2 =/
    Last edited by quad64bit; 11-09-2012 at 05:45 PM.

Similar Threads

  1. Example Count From Array Snippet
    By Vinx in forum Reviews / Advertising
    Replies: 1
    Last Post: 05-18-2012, 05:22 AM
  2. Duplicate count in array
    By louboulos in forum New To Java
    Replies: 2
    Last Post: 05-10-2012, 05:37 PM
  3. How to count records WITHOUT an array?
    By teekei in forum New To Java
    Replies: 17
    Last Post: 07-25-2011, 12:42 PM
  4. i can do count, but having problem with array
    By judy318 in forum New To Java
    Replies: 4
    Last Post: 10-29-2009, 09:23 PM
  5. Array count number Occurances
    By gwithey in forum New To Java
    Replies: 2
    Last Post: 04-17-2009, 08:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •