Counting the number of occurrences of each item in a list?

Up vote 2 down vote favorite 1 share g+ share fb share tw.

I have a streaming input which has repeated values. I can use any data structure but I have to count the number of occurence of each element. Suppose I have a list of mobile phone suppliers like the following: Apple Nokia Samsung Apple LG Nokia HTC Android Apple Nokia Nokia Apple Samsung I have to build any data structure preferably a map with details like Apple,4 Nokia,4 Samsung,2 LG,1 Android,1 I am not sure whether this is optimal.

Is there a better solution than this? In fact I have yet to write the above as a code. So better code will also help.

Java data-structures collections link|improve this question edited Jun 29 '09 at 16:45Michael Myers?43.2k1198167 asked Jun 29 '09 at 16:03Harish630319 79% accept rate.

Counting the list items" seems misleading – Tom Jun 29 '09 at 16:05.

Yes, I would use a Map. I would wrap the add in something like this: private static void incrementValue(Map counters, String toAdd) { Integer currValue = counters. Get(toAdd); if (currValue == null) counters.

Put(toAdd, 1); else counters. Put(toAdd, currValue+1); } Or without generics: private static void incrementValue(Map counters, String toAdd) { Integer currValue = (Integer) counters. Get(toAdd); if (currValue == null) counters.

Put(toAdd, 1); else counters. Put(toAdd, currValue+1); }.

A small information...I can't use Generics as I have to use Java 1.4 – Harish Jun 29 '09 at 16:15 cool it works and thanks for that – Harish Jun 29 '09 at 16:42.

Since it was mentioned by the questioner that generics could not be used, as the target platform was Java 1.4, one could use the Apache Commons Collections which doesn't use generics. The answer by pjp mentions that a Bag can be used. It turns out, the Apache Commons Collections has a Bag which has a getCount method which will return the count of a certain object that was added to the Bag.

The following is an example that adds some Integer objects to a HashBag, and counts how many of each Integer object that the Bag contains: Bag be = new HashBag(); b. Add(Integer. ValueOf(1)); b.

Add(Integer. ValueOf(2)); b. Add(Integer.

ValueOf(2)); b. Add(Integer. ValueOf(3)); System.out.

Println("Count for 1: " + b. GetCount(Integer. ValueOf(1))); System.out.

Println("Count for 2: " + b. GetCount(Integer. ValueOf(2))); System.out.

Println("Count for 3: " + b. GetCount(Integer. ValueOf(3))); The results were: Count for 1: 1 Count for 2: 2 Count for 3: 1 (I should add a disclaimer that this code was actually compiled and run on Java 6, but I believe I've only used features which were present from the pre-Java 5 days.

).

Thats brilliant...I would like to vote for you but I am yet to get reputation...Thanks for the response – Harish Jun 29 '09 at 16:41 +1, and this should be the accepted answer. The only possible reason not to do this is a fear of external libraries (which I have myself). – Michael Myers?

Jun 29 '09 at 16:42.

If a db - you can do this very easily in the query on the backend with group by.

Nope its from a flat file – Harish Jun 29 '09 at 16:17.

A map seems the way to go. Direct access :) Key: Element value: number of ocurrences, or a list with indexes of the element in the list.

Besides the solutions that have been posted the first thing that comes to my mind is to make a table "code - value" and encode the list using codes. It would be very space efficient.

The most natural structure for this is a Bag aka a Multiset. A bag is essentially a function from Object to Count. Google collections has a Multiset however you could easily build your own using a HashMap.

google-collections.googlecode.com/svn/tr....

– Harish Jun 29 '09 at 16:20 Note that Google Collections requires Java 5, though. Other than that, this is even easier to do than my answer. – Michael Myers?

Jun 29 '09 at 16:23 You can get the count by interating over the entrySet(). If you want to stream the count out you could extend the implementation to notify a listener when the count changes. – pjp Jun 29 '09 at 16:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions