Get duplicates from a list of String

Hi I have a list of string called list_str_RouteName. This value is:
{
“Route: 404”,
“Route: 404”,
“Route: 130”,
“Route: 201”,
“Route: 270”,
“Route: 464”,
“Route: 270”,
“Route: 407”,
“Route: 433”,
“Route: 464”,
“Route: S4”,
“Route: N109”
}
How do I get the duplicate values so I am left with:
{
“Route: 404”,
“Route: 270”,
“Route: 464”
}
Thanks for any help :slight_smile:

list_str_RouteName.GroupBy(Function (x) x).Where(Function (x) x.Count > 1).Select(Function (x) x.First()).toList

2 Likes

Hi @Karan_Chauhan

list_str_RouteName = {
    "Route: 404",
    "Route: 404",
    "Route: 130",
    "Route: 201",
    "Route: 270",
    "Route: 464",
    "Route: 270",
    "Route: 407",
    "Route: 433",
    "Route: 464",
    "Route: S4",
    "Route: N109"
}

duplicatesList = list_str_RouteName.GroupBy(Function(x) x).Where(Function(g) g.Count() > 1).Select(Function(g) g.Key).ToList()

Hope it helps!!

In Query Syntax
listDups =

(From x in list_str_RouteName
Group x by k=x.Trim() into grp=Group
Where grp.Count > 1
Select v = grp.First()).toList

This was the result. I put the code in an assign to list_duplicate is that wrong?
image

This was the result, I put it in an assign to list_duplicates
image

I put it in an assign yet this was the result
image
Could I be missing something? packages etc?

there are no additional packages needed as it is provided by .Net itself

Debug and trace with the debugging panel. Use the immediate panel for RnD and Prototyping:

Ensure that the input list is not empty and has duplicates

Verification:

Hi @Karan_Chauhan
duplicatesList variable’s datatype would be List(Of String)

Use the below syntax to print it in log message or message box.

String.Join(", ", duplicatesList)

Hope it helps

Hi that worked thank you!

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