How to count duplicate strings in an array

Hello,

I have an array, containing some strings split by a comma. I would like to count the amount of duplicate strings and set the number to a variable with the name of the item that appears the most. An example array is below:

“Postage”, “Registry”, “Registry”, “Registry”, “Registry”, “Search Fees”, “Registry”, etc.

The strings in the array will differ almost every time, as will the amount in the array, so I cannot look for specific words or locations. Does anybody have any suggestions on counting the duplicate strings?

Thanks in advance :slight_smile:

1 Like

Hi @william.coulson,

This post may help you, How to remove duplicate values within a string variable

1 Like

@william.coulson
find starter help here:
Array_ItemOccurence_CountReport.xaml (6.5 KB)

getting the duplicates only you can modify e.g.:
(From x In arrValues
Group x By k=x Into grp=Group
Where grp.Count > 1
Select dtReport.Rows.Add({k,grp.Count})).CopyToDataTable

3 Likes

Hi @ppr,

I am getting a validation error:

All the variable types are the same as the workflow you attached

I believe just an empty parenthesis at the end should help. So have it be .CopyToDataTable() at the very end.

Or perhaps it’s .ToTable() - I can never remember the syntax

1 Like

Hi @Dave,

Neither of those worked, unfortunately

@william.coulson
ensure following:

handling empty result (e.g. no duplicates are available and nothing is to add to the statistic datatable) can be done with applying following pattern:

2 Likes

Here’s how I would do it: parse the string and create a dictionary with the words as the keys and the values as init counters. If the new work is in the dictionary, increase the counter of that key. Of not, add the new key with the value 1.

1 Like

That’s essentially what the linq statement provided by ppr earlier in the thread does

1 Like

@william.coulson -I just tried whatever @ppr has provided and printed the output. It working perfectly. Please follow the below activities as is:

Write Line: "Count of " +row(“Value”).ToString + " is = " +row(“Count”).ToString

Output
image

1 Like

Hey @ppr,

I followed the steps in the topic you linked, and the validation error went away. I tried with the array you and @prasath17 used and my array of strings which worked brilliantly.

Thank you everybody for your time and your help!

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