How to count occurrence of each element in a list

I have a list List1 = new list(Of String) from {“John”,“David”,“Miller”,“John”,“Miller”,“Mary”,“John”}

The output should like

John: 3
David: 1
Miller: 2
Mary: 1

My logic was to have a for-each loop to parse List1 and add distinct elements to a new list List2 and each time check List2 if the element exists and if so increment a counter, but somehow couldnt get this flow in uipath.

Could somebody help verify if the above logic is correct and if so help me with the flow to achieve this.


you can use




There are some ways to achieve it. For now I’ll suggest 2 ways as the following.

The first way is to use Dictionary and loop.
The second way is to use LINQ

dict = listStr.GroupBy(Function(s) s).ToDictionary(Function(g) g.Key,Function(g) g.Count) (2.8 KB)


1 Like

Hello @mcashwin Please check the below post. It would help you!!

Thanks Yoichi-san, well thats what I was looking for.

Just a basic question as I have not worked on linq’s before, in the below what are these parameters “s” and “g” are they constants?

dict = listStr.GroupBy(Function(s) s).ToDictionary(Function(g) g.Key,Function(g) g.Count)


They are anonymous function of lambda expression. The following document will help you.


In the same manner, would it be possible to move distinct item in list 1 to list2 and have a counter to track count of each element


Probably yes. For specific expression, can you share specific example (input and expected output)?


So the solution you provided was moving the list items to dictionary and displaying them, which is excellent. On the same lines I am trying to do is below

  1. Define List1 ={“John”,“Terry”,“John”,“Mike”,“Terry”,“John”}
  2. Parse through each of the above element in list1 using for each
  3. Within body of for-each, check if List2.Contains(item)
  4. If true, have a counter and increment it
  5. If false, add to collection and set counter to 1
  6. Finally at the end another for each to parse List2 and print the element followed by its count of occurance for eg: John 3 Terry 2 Mike 1
    I am able to get the contents into List2, but the count doesnt seem to come correctly. secondly do you think i should use diff indices in each for loop like item1 and item2?
    What do you think about the above steps, are the okay


Can you share your workflow, if possible?


Please find attached

List_2.xaml (10.5 KB)


There is only single counter variable : cnt in the workflow. It cannot have count for each item. To solve this, try to use dictionary etc.


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.