I have a List<string>
which has some words duplicated. I need to find all words which are duplicates.
Any trick to get them all?
I have a List<string>
which has some words duplicated. I need to find all words which are duplicates.
Any trick to get them all?
and without the LINQ:
If you are using LINQ, you can use the following query:
or, if you prefer it without the syntactic sugar:
This groups all elements that are the same, and then filters to only those groups with more than one element. Finally it selects just the key from those groups as you don't need the count.
If you're prefer not to use LINQ, you can use this extension method:
This keeps track of items it has seen and yielded. If it hasn't seen an item before, it adds it to the list of seen items, otherwise it ignores it. If it hasn't yielded an item before, it yields it, otherwise it ignores it.
Using LINQ, ofcourse. The below code would give you dictionary of item as string, and the count of each item in your sourc list.
If you're looking for a more generic method:
EDIT: Here's an example:
I'm assuming each string in your list contains several words, let me know if that's incorrect.