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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:57:55+00:00 2026-06-13T22:57:55+00:00

Is there any easy way to get the keys for which same value exist?

  • 0

Is there any easy way to get the keys for which same value exist?
Or more importantly, how can i get the number of same-value-more-than-once occurrences?

Consider the hashmap:

1->A
2->A
3->A
4->B
5->C
6->D
7->D

here same-value-more-than-once occurred 3 times(A two times, D one time).That(3) is what i want in return.

I could iterate over the hashmap by the keyset/map.values() list, but it seems quite cumbersome to do that way. Any suggestions or solutions?

EDIT :
My context is, i’m working on a timetable generator. The data-structure for a time-slot is

{String day-hour, HashMap<String,Event> Rooms}

For a day-hour, some Event-s are assigned on Rooms map. While checking the fitness of the solution, i need to know if one staff is assigned multiple events on same hour. Hence i want to check how many violations are there in Rooms map by the values Event.getStaff() .

EDIT :
Values are objects here, I don’t want to count the occurrences of the same objects, rather a field of the object. The EVENT object has a field staff and i need to count the multiple occurrences of staffs.

  • 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-13T22:57:56+00:00Added an answer on June 13, 2026 at 10:57 pm

    This is nice way I think:

    int freq = Collections.frequency(map.values(), "A");
    

    which returns “3” for your example. Cheers!

    EDIT: sorry I misunderstood the question in my first attempt, this should do the trick:

    int k = 0;
    Set<String> set = new HashSet<String>(map.values());
    for (String s : set) {
       int i = Collections.frequency(map.values(), s);
       k += i > 1 ? i - 1 : 0;
    }
    

    You will still not be able to retreive the actual keys though. But that was not the most important thing, right?

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

Sidebar

Related Questions

Is there any easy way to get the number of rows returned by a
Is there any easy way to get the hightest numeric value of an associative
Is there any easy way using the api to get a count of all
Is there any easy way to calculate the number of lines changed between two
Is there any easy way to get the string index in an NSString if
Hey, is there any easy way to get information when user will change time
Is there any easy way to get the list of classes from VS.Net 2008
Is there any easy way to get the averages of items in a list
Is there any easy way to get all failing test caused by my recent
Is there any easy way to get Windows Edition (Home, Professional, 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.