Click here to Skip to main content
16,008,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a list object objList with two list of values

C#
foreach (object eachItem in objList )
       {
       }


How to find any duplicate value is there inside the objList
Posted

There are a number of ways to do this, and the easiest is to use Linq.
But...since you seem to be a beginner you may not have reached that yet, and it's pretty simple to find duplicates if you think about it for a moment.
The List<T> collection has a Sort method[^], and if you order your values then duplicates will be next to each other:
C#
List<string> objList = new List<string>() {"A", "C", "B", "Z", "B"};
obList.Sort();
Once they are sorted, duplicates are next to each other, so it's a simple matter to find duplicates:
C#
string last = "";
foreach (object eachItem in objList )
    {
    if (eachItem == last)
        {
        Console.WriteLine("Duplicate: {0}", eachItem);
        }
    last == eachItem;
    }
 
Share this answer
 
Comments
Thanks7872 15-Dec-14 4:28am    
Great....great example..+5
var duplicateByLinq = objList.GroupBy(x=>x).Where(x=>x.Count() >1).Select(group=>Group.Key));
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900