views:

150

answers:

6

I'm having a List<string> like:

List<String> list = new List<String>{"6","1","2","4","6","5","1"};`

I need to get the duplicate items in the list into a new list. Now i'm using nested for loop to do this.

The Resulting list will contain {"6","1"}

Is there any idea to do this using LINQ or Lambda expression?

+8  A: 
var duplicates = lst.GroupBy(s => s)
    .SelectMany(grp => grp.Skip(1));

Note that this will return all duplicates, so if you want only want to know which items are duplicated in the source list, you could apply Distinct to the resulting sequence or use the solution given by Mark Byers.

Lee
@Lee: Thank you....:-)
Pramodh
+6  A: 

Here is one way to do it:

List<String> duplicates = lst.GroupBy(x => x)
                             .Where(g => g.Count() > 1)
                             .Select(g => g.Key)
                             .ToList();

The GroupBy groups the elements that are the same together, and the Where filters out those that only appear once, leaving you with only the duplicates.

Mark Byers
Argghhh, you are all correct, sorry :)
leppie
leppie : I'm confused , all the answers are correct. which one will be marked as the correct answer
Pramodh
@Pramodh: Well Aamod's answer is the same as mine except it was posted later. Lee's answer is different to mine and it turns out that what you actually wanted was Lee's answer so I guess you had better mark that correct.
Mark Byers
+2  A: 

Hope this wil help

int[] listOfItems = new[] { 4, 2, 3, 1, 6, 4, 3 };

var duplicates = listOfItems 
    .GroupBy(i => i)
    .Where(g => g.Count() > 1)
    .Select(g => g.Key);

foreach (var d in duplicates)
    Console.WriteLine(d);
Aamod Thakur
+1  A: 

Here's another option:

var list = new List<string> { "6", "1", "2", "4", "6", "5", "1" };

var set = new HashSet<string>();
var duplicates = list.Where(x => !set.Add(x));
LukeH
@ LukeH :+1 Nice solution
Pramodh
I don't suppose the downvoter would care to explain what's wrong with this answer?
LukeH
+1  A: 
  List<String> list = new List<String> { "6", "1", "2", "4", "6", "5", "1" };

    var q = from s in list
            group s by s into g
            where g.Count() > 1
            select g.First();

    foreach (var item in q)
    {
        Console.WriteLine(item);

    }
mumtaz
+1  A: 

I wrote this extension method based off @Lee's response to the OP. Note, a default parameter was used (requiring C# 4.0). However, an overloaded method call in C# 3.0 would suffice.

  /// <summary>
  /// Method that returns all the duplicates (distinct) in the collection.
  /// </summary>
  /// <typeparam name="T">The type of the collection.</typeparam>
  /// <param name="source">The source collection to detect for duplicates.</param>
  /// <param name="distinct">Specify <b>true</b> to only return distinct elements.</param>
  /// <returns>A distinct list of duplicates found in the source collection.</returns>
  /// <remarks>This is an extension method to IEnumerable&lt;T&gt;</remarks>
  public static IEnumerable<T> Duplicates<T>(this IEnumerable<T> source, bool distinct = true)
  {
     if (source == null)
     {
        throw new ArgumentNullException("source");
     }

     // select the elements that are repeated
     IEnumerable<T> result = source.GroupBy(a => a).SelectMany(a => a.Skip(1));

     // distinct?
     if (distinct == true)
     {
        // deferred execution helps us here
        result = result.Distinct();
     }

     return result;
  }
Michael