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

  • Home
  • SEARCH
  • 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 9248157
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:53:35+00:00 2026-06-18T09:53:35+00:00

Write a method counts that accepts a List of integers and a Set of

  • 0

Write a method counts that accepts a List of integers and a Set of integers as parameters, and returns a map from each value in the set to the number of occurrences of that value in the list.

My solution #1:

public static Map<Integer, Integer> counts(List<Integer> list, Set<Integer> set) {
    Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
    Iterator<Integer> i = list.iterator();

    for(Integer element : set) {
        int count = 0;
        for(Integer sub : list) {
            if(sub == element) {
                count++;
            }
        }
        map.put(element, count);
    }

    return map;
}

My solution #2:

public static Map<Integer, Integer> counts(List<Integer> list, Set<Integer> set) {
    Map<Integer, Integer> map = new TreeMap<Integer, Integer>();
    Iterator<Integer> i = list.iterator();

    for(Integer element : set) {
        int count = 0;
        while(i.hasNext()) {
            if(i.next() == element) {
                count++;
            }
        }
        map.put(element, count);
    }

    return map;
}

Input:

list: [4, -2, 3, 9, 4, 17, 5, 29, 14, 87, 4, -2, 100]
set: [-2, 4, 29]

Expected output:

{-2=2, 4=3, 29=1}

Output for #1:

{-2=2, 4=3, 29=1}

Output for #2:

{-2=0, 4=3, 29=0}

The first one works, but the second one does not. Why? They are essentially the same thing, or am I missing anything? Also, would using a for loop work to traverse through the list? If not, why?

  • 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-18T09:53:36+00:00Added an answer on June 18, 2026 at 9:53 am

    You forget to reset the iterator after each loop, try the following:

    for(Integer element : set) {
        int count = 0;
        i = list.iterator(); // resets the iterator to the first element. 
        while(i.hasNext()) {
            if(i.next() == element) {
                count++;
            }
        } // by the end of this loop the iterator has no next elements, need to reset.
        map.put(element, count);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Write a method called wordCount that accepts a String as its parameter and returns
I am trying to write a method that converts from engineering notation to double
I'm asked to create a method that returns the number of occurrences of a
i write a method that loops through an array of buttons and checks if
How do you write a method/message with multiple parameters? EDIT: Like multiple parameters for
I wanted to write a method with an argument that defaults to a member
I want to write a method that run multiple threads and I want before
I'm trying to write a method that will compute all permutations of a power
I want to write generic method that checks if a given entity is in
I have a method that receives messages from a queue of msmq. I have

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.