In my below program I have to find the single duplicate number. For example (from the series 3, 5, 3, 2, 7, 7, 5, 6) if 3 is repeated then program should exit and return 3. If not then go for next number 5, if it's repeated then should exits and return 5.
I am getting all repeated numbers 3,5,7 but I want to find the single number which is repeated first. How should I get this?
class Program
{
private string checkduplicate()
{
List<int> list = new List<int>() { 3, 5, 3, 2, 7, 7, 5, 6 };
IEnumerable<int> duplicates = list.GroupBy(x => x)
.Where(g => g.Count() > 1)
.Select(x => x.Key);
return String.Join(",", duplicates);
//var list = new List<int>() { 3, 5, 3, 2, 7, 7, 5, 6 };
//list.GroupBy(n => n).Any(c => c.Count() > 1);
}
static void Main(string[] args)
{
Program p = new Program();
Console.WriteLine(p.checkduplicate());
Console.ReadLine();
}
}