Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8286685
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T11:50:43+00:00 2026-06-08T11:50:43+00:00

I am stack for a while . I tried debugging but I couldn’t figure

  • 0

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");
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T11:50:45+00:00Added an answer on June 8, 2026 at 11:50 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have tried looking in other areas, but I can't seem to figure out
We've been debugging this JBoss server problem for quite a while. After about 10
I've been trying to piece together how stack memory is handed out to threads.
I'm kinda new to Scala trying it out while reading Beggining Scala by David
I'm trying to tempalitize a linkedlist(/stack/queue) to accept int and double. But so far
Trying a mental reset here: I tried to create a reliable, persistent stack with
I have been stuck on this for a while. i have tried and I
How i can know the current method stack frame while a recursive call in
I'm using Rails on Heroku Cedar stack and it's not throwing any issues while
I stuck while trying to compile qpid c++ with boost 1_47_0 using Visual Studio

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.