• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array List

 
James Tok
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good day,
I have a list as below

----------
A
A
B
B
B
C
C
C
----------

how can i get something as below: which the number indicates the # repeated in the list?
----------
A,2
B,3
C,3
----------

i only know that i could use HashSet to eliminate the duplicates... but how to display the number?

Thank.

 
Carey Brown
Bartender
Posts: 1680
22
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Instead of HashSet, use HashMap with a value of type Integer to keep the count in.
 
Darryl Burke
Bartender
Posts: 5148
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to use a java.util.Map
 
fred rosenberger
lowercase baba
Bartender
Posts: 12196
35
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will need to build the data. What the above folks are suggesting is as follows...

define a map with a String as the key, and an Int as the value.

read through your array, one at a time.

For each element, check the map to see if the current element exists as a key

If not, add it in with '1' as the value

If so, increment the counter that currently exists.

When you have gone through the array list, you can get a list of all the keys, and then use that to get all the values.


OR...

If your input will definitely be nothing more than single characters, you can accomplish the same thing with a 26 element array of ints.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic