Problem:
I have one ArrayList which contains a lot of duplicate values. I need to create a function to remove duplicate values from the ArrayList. My ArrayList contains objects of same type.
Solution:
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);
I have one ArrayList which contains a lot of duplicate values. I need to create a function to remove duplicate values from the ArrayList. My ArrayList contains objects of same type.
Solution:
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);
No comments:
Post a Comment