Doesn't Matter
Doesn't Matter

Reputation: 405

counting occurrence of numbers in array

I am stack for a while . I tried debugging but I couldn't figure out the solution. I am trying to count the occurrence of numbers. So my problem is that when I print the output it is

3 occurs 1 times
1 occurs 1 times
0 occurs 1 times
2 occurs 1 times
1 occurs 2 times
3 occurs 2 times
2 occurs 2 times
0 occurs 2 times
10 occurs 1 times
4 occurs 1 times

instead of

1 occurs 2 times
0 occurs 2 times
2 occurs 2 times
3 occurs 2 time
10 occurs 1 times
4 occurs 1 times 

so if the number has more than 1 occurrence it should say it only once not as many times as there is occurrences. Cheers Here is the code

import java.util.*;

public class CountingOccuranceOfNumbers
{

    public static void main(String[] args) 
    {
        countNumbers();
    }

    public static void countNumbers()
    {
        Scanner input = new Scanner(System.in);
        Random generator = new Random();
        int[] list = new int[11];
        int[] counts = new int[150];
        int counter = 0;
        int number = 1;


        while(counter <= 10)
        {
                number = generator.nextInt(11);
                list[counter] = number;
                counter++;
        }   
        for(int i=0; i<list.length - 1; i++)
        {
            counts[list[i]]++;
//          System.out.print(list[i] + " ");

            System.out.println(list[i] +" occurs " +  counts[list[i]] + " times");
        }

    }

}

Upvotes: 0

Views: 18806

Answers (6)

cl-r
cl-r

Reputation: 1264

Use a HashMap<Integer>,<integer> ht to manage your counts

if (ht.get(newNumber) == null) {
     ht.put(newNumber, 1); 
} else {
     ht.put(newNumber, ++ht.get(newNumber));
}

Corrected HashTable in HashMap and ++ before get(..)

Upvotes: 3

Wai Hein
Wai Hein

Reputation: 69

Create a HashMap and put new Entry in the map with key,value where value is Integer.

if you come across with same char, then increment the integer value associated with that key. o.w it is a new key and set the value to 1.

Integer entryValue;
Map map = new HashMap();


    for ( int i =0; i < s1.length(); i++)
    {
        entryValue = (Integer)map.get(s1.charAt(i));

        if (entryValue == null)
        {
            map.put(s1.charAt(i), new Integer(1));
        }

        else
        {
                map.put(s1.charAt(i), new Integer(entryValue.intValue()+1));
        }
    }

Upvotes: 0

user3061612
user3061612

Reputation: 11

    import java.io.BufferedReader;
    import java.io.InputStreamReader;


    public class NumberRepetition {



    public static void main(String[] args) throws Exception {
        int size;
    BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("enter size of array");
    size=Integer.parseInt(br.readLine());
    int el;
    int[] a=new int[size];
    for(int i=0;i<size;i++)
    {
        System.out.println("enter a number");
        el=Integer.parseInt(br.readLine());
        a[i]=el;
    }
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size-1;j++)
        {
            if(a[j]>a[j+1])
            {
                int temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;

            }
        }
    }
    int count=0;
    for(int i=0;i<size;i++)
    {
        for(int j=0;j<size;j++)
        {
            if(a[i]==a[j])
            {
                count++;

            }



        }
        System.out.println(a[i]+" \t " +"occurence time is"+"\t"+count);
        if(count!=0)
        {
            i=i+count-1;
        }
        count=0;
    }

}

}

Upvotes: 0

Matt
Matt

Reputation: 11805

Another option is guava's Multiset classes, which will track the count for you:

int values[] = ...;
Multiset<Integer> ms = HashMultiset.create();
ms.addAll(Ints.asList(list));

int count0 = ms.count(Integer.valueOf(0));
int count1 = ms.count(Integer.valueOf(1));

Here, Multiset, HashMultiset and Ints are all guava classes.

Note that Multiset does pretty much what someone mentioned above by using a Map and counter to track the counters. It's just abstracted away from you to make your code simpler.

Upvotes: 3

Peter Lawrey
Peter Lawrey

Reputation: 533442

You have one loop to count the occurrence which also give a running total. It appears what you wanted is to only print the total when the counting is finish. i.e. it should be in another loop.

Upvotes: 2

Keppil
Keppil

Reputation: 46209

Ok, I'll try to give you a hint or two.

  1. Since you get several lines printed for each number that occurs more than once, you probably shouldn't print anything until you are done with your counting.
  2. It looks like your output should be sorted after number of occurences. If this is the case, saving the counts in an array probably isn't the best idea. Consider using a Map<Integer, Integer> instead, where the key is the number, and the value is the number of occurences.

Upvotes: 1

Related Questions