How to remove Duplicate elements from ArrayList in C#

No.of Views7921
Bookmarked0 times
Downloads 
Votes0
By  RRaveen   On  21 Jun 2010 22:06:46
Tag : CSharp , Miscellaneous
In this code snippet, I have describe how to remove the duplicate elements from the arraylist
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

In this code snippet, I have described how to remove the duplicate elements from the arraylist. I have created a sample console application. Then create an ArrayList with 5 elements, within the five elements, once element is duplicating as like following,

ArrayList list = new ArrayList();list.Add("Rave");list.Add("Kumar");list.Add("Tin");list.Add("Rave");list.Add("Murugan");

 In above Array List “Rave” element is presented two times. Next steps to compose a method to remove that duplicate element.

private static ArrayList RemoveDuplicate(ArrayList sourceList){ArrayList list = new ArrayList();foreach (string item in sourceList){if (!list.Contains(item)){list.Add(item);}}return list;}

Let’s write method to print the output in console before remove the duplicate and after remove the duplicates.

private static void PrintLines(ArrayList list){foreach (string item in list){Console.WriteLine(item);}}

 Complete code

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication1
{class Program{static void Main(string[] args){ArrayList list = new ArrayList();list.Add("Rave");list.Add("Kumar");list.Add("Tin");list.Add("Rave");list.Add("Murugan");Console.Write("before remove duplicates");Console.WriteLine(Environment.NewLine);PrintLines(list);Console.Write("After removed");Console.WriteLine(Environment.NewLine);PrintLines(RemoveDuplicate(list));        

            Console.ReadLine();}private static void PrintLines(ArrayList list){foreach (string item in list){Console.WriteLine(item);}}private static ArrayList RemoveDuplicate(ArrayList sourceList){ArrayList list = new ArrayList();foreach (string item in sourceList){if (!list.Contains(item)){list.Add(item);}}return list;}}}

 

Output

before remove duplicates

Rave
Kumar
Tin
Rave
Murugan

After removed

Rave
Kumar
Tin
Murugan

Way 2

If its Array List has elements as string type, there is another way to remove the duplicates without using contains methods in array list.I have used HashSet delete the duplicate in this way. The HashSet<T> class provides high performance set operations. A set is a collection that contains no duplicate elements, and whose elements are in no particular order.about HashSet here

private static string[] RemoveDuplicateWay2(ArrayList sourcelist){HashSet<string> set = new HashSet<string>((string[])sourcelist.ToArray(typeof(string))); //http://msdn.microsoft.com/en-us/library/bb359438.aspxstring[] result = new string[set.Count];set.CopyTo(result);return result;}

Call this method and pass source arraylist as argument.

The Final Code

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Collections;namespace ConsoleApplication1
{class Program{static void Main(string[] args){ArrayList list = new ArrayList();list.Add("Rave");list.Add("Kumar");list.Add("Tin");list.Add("Rave");list.Add("Murugan");Console.Write("before remove duplicates");Console.WriteLine(Environment.NewLine);PrintLines(list);Console.Write("After removed");Console.WriteLine(Environment.NewLine);PrintLines(RemoveDuplicate(list));Console.Write("way 2 remove the duplicate if its  string array list");Console.WriteLine(Environment.NewLine);PrintLines(RemoveDuplicateWay2(list));Console.ReadLine();}private static void PrintLines(ArrayList list){foreach (string item in list){Console.WriteLine(item);}}private static void PrintLines(string[] list){foreach (string item in list){Console.WriteLine(item);}}private static ArrayList RemoveDuplicate(ArrayList sourceList){ArrayList list = new ArrayList();foreach (string item in sourceList){if (!list.Contains(item)){list.Add(item);}}return list;}private static string[] RemoveDuplicateWay2(ArrayList sourcelist){HashSet<string> set = new HashSet<string>((string[])sourcelist.ToArray(typeof(string))); //http://msdn.microsoft.com/en-us/library/bb359438.aspxstring[] result = new string[set.Count];set.CopyTo(result);return result;}}}

 The Final Output

before remove duplicates

Rave
Kumar
Tin
Rave
Murugan

After removed

Rave
Kumar
Tin
Murugan


way 2 remove the duplicate if its  string array list

Rave
Kumar
Tin
Murugan

I hope this is help to you all and save the time.

 
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