473,503 Members | 1,725 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Count unique values in ArrayList C#

GTXY20
29 New Member
Hi all,

I have an ArrayList1 of multiple values. Here is an exaple of the a potential ArrayList1:

a,a,b,,c,a,b,c,c,c,a,b

Does anyone have any suggestion whereby I could output the count for wach unique item:

a=4
b=3
c=4

I have done this in python with a defaultdict(int) and was wondering if there was a quick way in C# to do this other than creating another ArrayList2 containing a unique set of the items from all od the items in ArrayList1 and then iterating through ArrayList1 counting the values for i where i is drevied from ArrayList2.

Any guidance or a starting point is much appreciated.

G.
Apr 13 '08 #1
2 12512
GTXY20
29 New Member
The list of values were kept in a dictionary so I have a dictionary with a key value pair where the value is a list of items.

Expand|Select|Wrap|Line Numbers
  1. data = new Dictionary<string, List<string>>
  2.  
I ended up placing the items from the value list of the dictionary into two additonal lists a list of distinct values (itemsunique) and then used a foreach loop on a complete list of all the values for all keys in the (allitems):

Expand|Select|Wrap|Line Numbers
  1. List<string> allitems = new List<string>();
  2. List<string> itemsunique = new List<string>();
  3.  
  4. foreach (KeyValuePair<string, List<string>> s in data)
  5. {
  6. foreach (string h in s.Value)
  7. {
  8. allitems.Add(h);
  9. allitems.Sort();
  10.  
  11. if (!itemsunique.Contains(h))
  12. {
  13. itemsunique.Add(h);
  14. itemsunique.Sort();
  15. }
  16.  
  17. }
  18. }
  19.  
  20.  
  21. int count = 0;
  22. foreach (string y in itemsunique)
  23. {
  24. count = allitems.count(n=> n == y);
  25. Console.Writeline("{0}={1} ", y, count);
  26. }
  27.  
However I will have several lists that I will need to apply this to and afraid that this will be memory intensive if I have to do with each list considering some lists will be quite large with over 100k items.

Any suggestions or comments are appreciated.

Thanks.

g.
Apr 14 '08 #2
GTXY20
29 New Member
Further I was able to do the same using a hashtable:

Expand|Select|Wrap|Line Numbers
  1.  
  2. Hashtable items = new Hashtable();
  3. if (!items.Contains(itemfromlist))
  4.        items.Add(itemfromlist, 1);
  5. else
  6.        items[itemfromlist] = (int) items[itemfromlist]+1
  7.  
Apr 14 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
5953
by: Michael Krzepkowski | last post by:
All, I have a view that returns the following values: Item Vendor 70807 1234 70807 5678 If I am looking for items that have more...
6
10599
by: M.Kamermans | last post by:
I have an XSLT script that has to operate on some particularly nasty XML to turn it into a SQL databse, but 'entries' in the XML aren't actually unique, so their unique identifier is useless. I...
0
2124
by: kovac | last post by:
The System.directoryservices.dll has an error, and this error was described in http://support.microsoft.com/default.aspx?scid=kb;en-us;839424 At the moment we have Framework version v1.0.3705 and...
2
7791
by: SJM | last post by:
I have a report that displays records of real estate properties. It is possible for each property to appear a number of times for various reasons. Each record however is unique. What I would like...
9
10807
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
3
3856
by: Eric Cathell | last post by:
I am using an arraylist to process data....the unique problem i have run into is that if I delete one of the indexed values the arraylist is reindexed and i get an exception thrown. I need to start...
22
12423
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
1
3496
newnewbie
by: newnewbie | last post by:
Desperately need help in creating a query to count unique values in a table. I am a Business analyst with limited knowledge of Access….My boss got me ODBC connection to the underlying tables for our...
18
3416
by: Tony | last post by:
Hello! I have an ArrayList with many double that is sorted some of these double value exist more then once in the ArrayList. Is it possible to use some feature in C# or from the framework to...
0
7202
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
382
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.