How to Remove Duplicate Elements from Generic List using c#

No.of Views1275
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  13 Dec 2010 19:12:26
Tag : CSharp , Miscellaneous
This is very simple snippets to remove the duplicate or repeating element in generic list using C#. The Generic list is good collection to support multiple data type and reduce the casting operation.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

 Introduction

This is very simple snippets to remove the duplicate or repeating element in generic list using C#. The Generic list is good collection to support multiple data type and reduce the casting operation.In one my project I have been use the generic list for keep the few object with different types. But sometime elements are repeating in list. So I have created a method to remove the repeating element from list.

Implementation

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
    class Utility
    {
        
        public static List<T> RemoveRepeatElement<T>(List<T> source)
        {
            Dictionary<T, int> listofUniqueElement = new Dictionary<T, int>();
            List<T> listofdest = new List<T>();

            foreach (T item in source)
            {
                if (!listofUniqueElement.ContainsKey(item))
                {
                    listofdest.Add(item);
                    listofUniqueElement.Add(item, 0);
                }
            }
            return listofdest;
        }
    }
}

In above method I have been used Dictionary to keep the unique element as key to check every element with looping. If an element not found inside the dictionary, then I will add that into Dictionary and new List as well. finally return new list.

How to use?

using System;
using System.Collections.Generic;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<string> collectionOfSource = new List<string>();
            collectionOfSource.Add("RRaveen");
            collectionOfSource.Add("John");
            collectionOfSource.Add("RRaveen");
            collectionOfSource.Add("Kumar");
            collectionOfSource.Add("Dhana");
            Console.WriteLine("Source:");
            Console.WriteLine("---------------------------------");
            foreach (string var in collectionOfSource)
            {
                Console.WriteLine(var);
            }
            List<string> collectionOfremoved = Utility.RemoveRepeatElement(collectionOfSource);
            Console.WriteLine("Duplicate removed collection:");
            Console.WriteLine("---------------------------------");
            foreach (string var in collectionOfremoved)
            {
                Console.WriteLine(var);
            }
            Console.ReadLine();
        }
    }
}

Output

Source:
---------------------------------
RRaveen
John
RRaveen
Kumar
Dhana
Duplicate removed collection:
---------------------------------
RRaveen
John
Kumar
Dhana

Hopes help.thank you for reading.

 
Sign Up to vote for this article
 
About Author
 
RRaveen
Occupation-Software Engineer
Company-TGS
Member Type-Gold
Location-Singapore
Joined date-03 Jun 2009
Home Page-codegain.com
Blog Page-www.codegain.com
- B.Sc. degree in Computer Science. - 4+ years experience in Visual C#.net and VB.net - Obsessed in OOP style design and programming. - Designing and developing Network security tools. - Designing and developing a client/server application for sharing files among users in a way other than FTP protocol. - Designing and implementing GSM gateway applications and bulk messaging. - Windows Mobile and Symbian Programming - Having knowledge with ERP solutions
 
 
Other popularSectionarticles
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top