From the course: .NET Essentials: Working with LINQ

Unlock this course with a free trial

Join today to access over 22,500 courses taught by industry experts.

Find duplicate values

Find duplicate values

- [Lecturer] Earlier in this chapter, we looked at how to use the distinct method. Its purpose is to remove all the duplicate items from a sequence. But if you have the opposite scenario, or you want to find the duplicate. At first glance, it seems it's possible to use distinct and combine it with another set function to get the duplicate values, but that doesn't really work. Instead, we use a different technique to find the duplicates by using GroupBy. So let's start by looking at this code here. I'm calling it GetColors. And then I'm selecting on the hex value. And then I'm calling distinct here. So when I run this, I get back this IEnumerable string, and I see there's 138 items in there. Now this is the problem, we saw this earlier in this chapter, is I know there's 140 colors in my data source. But when I look for unique hex values, I'm only getting back 138. So now the question is what's wrong? What are those…

Contents