Last active
July 4, 2017 20:41
-
-
Save jfcarr/90ca85aa75c741b8c92137e0fd72d40c to your computer and use it in GitHub Desktop.
Remove duplicates from a list collection - implemented as an extension
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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