IntroductionIn 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 codeusing 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 2If 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. |