I want to do the opposite thing as here
I have a list and I know how to remove the duplicates. But I want to have an option where the user can select which duplicate to keep. Some query quere I can have a list that will only show the duplicates. Something like:
Lets say my list is:
"tom" "bob" "Frank" "bob" "Lacey" "Frank"
I know that if I use the distinct method I will get:
"tom" "bob" "Frank" "Lacey"
I don't know what method to I have to use to get:
"bob" "bob" "frank" "frank"
or to get
"bob" "frank"
cause those are the ones that repeat.
Duplicates : ArrayList allows duplicate values while HashSet doesn't allow duplicates values.
To remove duplicate values, click Data > Data Tools > Remove Duplicates. To highlight unique or duplicate values, use the Conditional Formatting command in the Style group on the Home tab.
You can use GroupBy
to filter out the items that only occur once, then flatten the remaining items back into a list:
var resultList = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.SelectMany(g => g)
.ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With