-1

I am trying to count the number of occurrences in an array of integers and return the amount of times each number was displayed. My code counts the right amount of occurrences, but it displays them more than once.

Here is the relevant code:

        int num = input.nextInt();
        int count = 0;
        int[] integers = new int[num];
        for(int i = 0; i < num; i++)    {
            System.out.print("Enter int 1-50: ");
            integers[i] = input.nextInt();
        }
        for(int i = 0; i < num; i++)    {
            for(int j = 0; j < num; j++)    {
                if(integers[i] == integers[j])  {
                    count++;
                }
            }
            System.out.println(integers[i] + " occurs "+count+" times.");
            count = 0;
        } 

The issue is that if a number occurs more than once, it displays that number more than once. For example, {1, 2, 2, 3} would print "2 occurs 2 times" twice. I understand why this happens but I'm wondering if there's a simple way to make sure these statements only print once.

user8913
  • 53
  • 1
  • 4
  • Well, you'd want to ignore any numbers you've already counted. However, if you take a step back, you'll realize that what you want is a _mapping_ of a number to its count. To use a keyword: you want a frequency map (aka cardinality map). Look it up und work your way from there :) – Thomas Aug 02 '19 at 16:11
  • Thanks, I'll research this! – user8913 Aug 02 '19 at 16:18
  • You can always convert it to list and use a tool to do the job for you! Have a look at this answer that can actually answer your question https://stackoverflow.com/questions/8098601/java-count-occurrence-of-each-item-in-an-array – Michael Michailidis Aug 02 '19 at 16:11
  • The question was "count occurances in array". My suggestion was convert it to list and have a look at the link which is the SAME question already answered – Michael Michailidis Aug 02 '19 at 16:48
  • This should be a comment, not an answer. If it is a duplicate question then [vote to close](http://stackoverflow.com/help/privileges/close-questions) as such and/or leave a comment once you [earn](http://meta.stackoverflow.com/q/146472) enough [reputation](http://stackoverflow.com/help/whats-reputation). If the question is not a duplicate then tailor the answer to this specific question. – Bhargav Rao Aug 02 '19 at 17:05

2 Answers2

0

Your problem is the print statement. You call print at every iteration. Instead, you can store that count value in a map:

1 -> 1
2 -> 2
3 -> 1

Where the key is the integer value you are counting, and the value is how many times it appears in the array. At the end you can just print the map:

for(int key : map.keySet()) {
    int value = map.get(key);
    System.out.println(key + " occurs "+value+" times.");
}

You can find a more elegant solution here [link]

0

Without using Map : If you input numbers will be within 1-50 then my approach will work

int a[]=new int[51];
        int b[]={4,7,8,5,4,50,5,5,4,44};
        for(int c:b){
            a[c]++;
        }
        for(int c:b){
            if(a[c]!=-1){
                System.out.println("value: "+c+" "+"count"+ a[c]);
            }
            a[c]=-1;
        }
GnanaJeyam
  • 2,780
  • 16
  • 27