Ocurrences of a string on a List of string

I am doing an automation in which I have a list of strings containing names, example: (“John M”, “Lily Ki”, “June I”, “John M”). In it there may be repeated names and I would like to know how I could count, for example, how many times “Jhon M” is found in the list. Is there a simple method or structure that I can do this with?

Hey @WSay

Thanks for your question. I am sure there are several ways of doing this but my solution would be to use 2 lists. The first list with all the values in it. The second list with only distinct/unique values.

For each over the unique list and filter the first list for each item and extract a count.
varList = New List(OF String) From {“Tom”, “Paul”, “Fred”, “Tom”, “Fred”, “Gorge”}
varDistinctList = varList.Distinct().ToList
Filter inside for each: varList.AsEnumerable().Where(Function(x) x.ToString.Equals(item.ToString)).Count

Attached is a sample workflow.
Main.xaml (6.4 KB)

2 Likes

Check following code:

yourValues= {“John M”, “Lily Ki”, “June I”, “John M”}
countOfJohn = yourValues.Where(Function(x) x = “John M”).count

1 Like

give a try on

Assign Activity
LHS: dictReport | DataType: Dictionary(Of String, Int32)
RHS:

(From x In Values
Group x By k=x Into grp=Group
Select g=grp.toArray).ToDictionary(Function (d) d.First(), Function (d) d.Count)

grafik
grafik

Find starter help here:
Collection_to_OccurenceCountDictionary.xaml (5.6 KB)

[Edited: helper XAMl and Code Adoptions]

1 Like

Thank you All, @krzysztof.szwed option seems the simplest and it works, but I appreciate everyone’s response and will consider your options for future scenarios. :slight_smile:

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