Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Agile forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

List contents - number of occurrences ?

 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
List al = new ArrayList();
al.add("James");
al.add("Bill");
al.add("Joe");
al.add("Bill");
al.add("James");
al.add("Joe");
al.add("James");
al.add("Joe");
al.add("steve");
al.add("Joe");

Which is the best optimized way to get each elements number of occurrences like James occurs 3 times, Joe occurs 4 times, Steve occur 1 time.
 
Henry Wong
author
Marshal
Pie
Posts: 20889
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is a pretty common homework problem... so why don't you tell us what you have done so far? And if still in the design phase, what algorithm you are currently considering?

Henry
 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am planning to use HashSet for storing unique values. Then i will create duplicate keys in the Map based on one of the previous thread.



Finally planning to check the map values with HashSet unique values for incrementing the counter value.
 
antty ge
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I finish it :


syso Result:
 
kri shan
Ranch Hand
Posts: 1460
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


'result' HashMap does not contain any elements. How containsKey() works here ?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic