How to Remove duplicates from a IEnumerable using C# or VB.NET

To Remove duplicates from a IEnumerable using C# or VB.NET you can use the snippet below.

Sample .NET 3.5 and newer

Sample C#

using System;
using System.Collections.Generic;
using System.Linq;

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            var myList = new List<string>
            {
                "1A", "1A", "1B", "1C", "1C", "1D", "1D"
            };

            myList = myList.Distinct().ToList();

            foreach (var item in myList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic
Imports System.Linq

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList = New List(Of String)() From { _
				"1A", _
				"1A", _
				"1B", _
				"1C", _
				"1C", _
				"1D", _
				"1D" _
			}

			myList = myList.Distinct().ToList()

			For Each item As var In myList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub
	End Class
End Namespace

Sample .NET 2.0

Sample C#

using System;
using System.Collections.Generic;

namespace de.Fesslersoft.DistinctIenumerbable
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> myList = new List<string>();
            myList.Add("1A");
            myList.Add("1A");
            myList.Add("1B");
            myList.Add("1C");
            myList.Add("1C");
            myList.Add("1D");
            myList.Add("1D");

            var newList = new List<String>();
            foreach (var item in DistinctIenumerable(myList))
            {
                newList.Add(item);
            }

            foreach (var item in newList)
            {
                Console.WriteLine(item);
            }
            Console.Read();
        }

        internal static IEnumerable<T> DistinctIenumerable<T>(IEnumerable<T> input)
        {
            var passedValues = new Dictionary<T, bool>();
            foreach (T item in input)
            {
                if (!passedValues.ContainsKey(item))
                {
                    passedValues.Add(item, false);
                    yield return item;
                }
            }
        }
    }
}

Sample VB.NET (autogenerated)

Imports System.Collections.Generic

Namespace de.Fesslersoft.DistinctIenumerbable
	Class Program
		Private Shared Sub Main(args As String())
			Dim myList As List(Of [String]) = New List(Of String)()
			myList.Add("1A")
			myList.Add("1A")
			myList.Add("1B")
			myList.Add("1C")
			myList.Add("1C")
			myList.Add("1D")
			myList.Add("1D")

			Dim newList = New List(Of [String])()
			For Each item As var In DistinctIenumerable(myList)
				newList.Add(item)
			Next

			For Each item As var In newList
				Console.WriteLine(item)
			Next
			Console.Read()
		End Sub

		Friend Shared Function DistinctIenumerable(Of T)(input As IEnumerable(Of T)) As IEnumerable(Of T)
			Dim passedValues = New Dictionary(Of T, Boolean)()
			For Each item As T In input
				If Not passedValues.ContainsKey(item) Then
					passedValues.Add(item, False)
					yield Return item
				End If
			Next
		End Function
	End Class
End Namespace

Please Share us:
Please rate this snippet

Leave a Reply