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 7846185
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:29:32+00:00 2026-06-02T17:29:32+00:00

I have a map of word frequencies Map<String, Integer> . I need to make

  • 0

I have a map of word frequencies Map<String, Integer>. I need to make a set of the least occurring words. Say the lowest occurring words all appeared twice, I need to make a set of all these twice-occurring words. So far I have:

public Set findRarest()
{
    int occurrence = 1000;  //high initial value for word length
    for (Map.Entry<String,Integer> item : wcMap.entrySet())
    {
        if (item.getValue() > occurrence);        //most likely for performance
        else if (item.getValue() == occurrence)
        {
            rarest.add(item.getKey());
        }
        else                                      //found new lowest count
        {
            rarest.clear();
            rarest.add(item.getKey());
        }
    }
    return rarest;
}

This seems a little convoluted to me. Are there native collection tools to get this done?

  • 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-02T17:29:35+00:00Added an answer on June 2, 2026 at 5:29 pm

    I don’t think your code even works as written. Two things:

    1. Initialize occurrence with Integer.MAX_VALUE instead of just some arbitrary large value.

    2. Update the value of occurrence whenever you find a word which occurs less often.

    Other than that, your solution is fine. I’m not sure you could get anything much cleaner restricting yourself to Java Collections Framework classes.

    Updated code:

    public Set findRarest()
    {
        Set<String> rarest = new HashSet<String>();
    
        int occurrence = Integer.MAX_VALUE;  //high initial value for word length
        for (Map.Entry<String,Integer> item : wcMap.entrySet())
        {
            if (item.getValue() == occurrence)
            {
                rarest.add(item.getKey());
            }
            else if ( item.getValue() < occurrence )
            {
                occurrence = item.getValue();
                rarest.clear();
                rarest.add(item.getKey());
            }
        }
        return rarest;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this HashMap: HashMap<String, Integer> m which basically stores any word (String) and
I have to store words and their corresponding integer indices in a hash map.
I have a map declared as follows: map < string , list < string
I have a Map that uses a Set for the key type, like this:
I have a simple table of list of words, as the word column is
I have map for replacement words: $map = array( 'word1' => 'replacement1', 'word2 blah'
Another small question about STL: i have Dictionary: map <string,vector <Wordy> > Dictionary; using
I have a problem with a string in C++ which has several words in
I have a map to count the occurrence of words in a file. I
I have a dictionary of about 50,000 words; each word has-many synonyms, antonyms, etc.

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.