473,657 Members | 2,355 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 12529
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
5971
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 than one vendor: select item_num,count(*)
6
10614
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 need to somehow create crosslinked tables using the XSLT, but can only do this by maintaining a counter for each line I write. Is there a way to create a proxy 'increment' function that lets me maintain a counter variable? - Mike Kamermans
0
2163
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 I have remove System.directoryservices.dll from current Framework version v1.0.3705. In state of old version I load die new System.directoryservices.dll from the Framework version v1.1.4322. I work with the following function of Joe Kaplan...
2
7818
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 to do is display the total of the number of unique properties in the report footer, not just a count of the number of records. I have experimented with grouping on the property field and using running sums but to no avail. I have also tried to...
9
10835
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 LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been exceeded....
3
3872
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 at the last item in the arraylist and go backwards....a for next loop doesnt seem to let me do this... for i =arraylist.count-1 to 0 'do stuff here next
22
12463
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
1
3507
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 system and thinks I am omnipotent now and can extract any data out of it in the form he wants….The truth is, though I know SOME Access, I am not a programmer…and many queries that he wants me to do have a potential of being monstrous towers of...
18
3425
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 count each unique double value or is the only solution to run through the list and count every unique double value. //Tony
0
8399
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8827
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8504
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8606
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7337
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6169
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4159
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2732
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1622
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.