Skip to content

Instantly share code, notes, and snippets.

@jfcarr
Last active July 4, 2017 20:41
Show Gist options
  • Save jfcarr/90ca85aa75c741b8c92137e0fd72d40c to your computer and use it in GitHub Desktop.
Save jfcarr/90ca85aa75c741b8c92137e0fd72d40c to your computer and use it in GitHub Desktop.
Remove duplicates from a list collection - implemented as an extension
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
namespace Console1
{
public static class ListExtension
{
public static IEnumerable<T> RemoveDuplicates<T>(this List<T> inputList)
{
inputList.Sort();
return inputList.Distinct();
}
}
class Program
{
static void Main(string[] args)
{
var myList = new List<string> {
"John",
"Andrew",
"James",
"Jack",
"Andrew",
"Bob",
"Jack"
};
var newList = myList.RemoveDuplicates();
foreach (var item in newList)
{
Console.WriteLine(item);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment