Subscribe to Subscribers

Forums » .NET » ASP.NET »

How to remove duplicate value from ARRAYLIST


Posted Date: 30 Jun 2012      Posted By:: Mitesh     Member Level: Silver    Member Rank: 743     Points: 2   Responses: 2



i have one arraylist wich contain values from different database but it stores some duplicate values so, i want to remove duplicate values and store only unique value in ARRay List.

So, how can this is achievable ?

Thanks in Advance... Mitesh




Responses

#678187    Author: Lalji      Member Level: Gold      Member Rank: 130     Date: 30/Jun/2012   Rating: 2 out of 52 out of 5     Points: 4

You can use LINQ distinct method to filter out duplicate values if ArrayList contains objects of same type.

using System.Collections;
using System.Linq;

ArrayList duplicateList= new ArrayList();
duplicateList.Add(10);
duplicateList.Add(20);
duplicateList.Add(10);
duplicateList.Add(30);
duplicateList.Add(40);
duplicateList.Add(50);
duplicateList.Add(40);
duplicateList.Add(20);

public ArrayList RemoveDuplicate(ArrayList duplicate)
{
ArrayList distinctArr= new ArrayList(); duplicate.ToArray().Distinct().ToList()
.ForEach(d=> distinctArr.Add();
return distinctArr;
}


ArrayList distinctList= new ArrayList();
distinctList = RemoveDuplicate(duplicateList);


 
#678274    Author: Siva Prasad      Member Level: Gold      Member Rank: 62     Date: 30/Jun/2012   Rating: 2 out of 52 out of 5     Points: 3


using System.Collections;
protected void Page_Load(object sender, EventArgs e)
{
ArrayList arrLs = new ArrayList { 1, 4, 15, 3, 7,4,1,3 };
ArrayList distinctArrLs = RemoveDuplicate(arrLs);
}


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


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Problem while inserting a values by a textbox which is in gridview
Previous : I want reduce my page like dialog box.and i have to disable the maximize button
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.