How to union data in ArrayList C# in dotnet framework 2?
example of data : 1, 2, 2, 3, 4, 5, 5, 6, 6
how to get 1, 2, 3, 4, 5, 6
How to union data in ArrayList C# in dotnet framework 2?
example of data : 1, 2, 2, 3, 4, 5, 5, 6, 6
how to get 1, 2, 3, 4, 5, 6
Hashtable htCopy = new Hashtable();
foreach (int item in arrListFull)
{
htCopy[item] = null;
}
ArrayList distinctArrayList = new ArrayList(htCopy.Keys);
// Assuming your data is an ArrayList called "source"
ArrayList dest = new ArrayList();
foreach(int i in source) if(!dest.Contains(i)) dest.Add(i);
You should be using List<int> instead of ArrayList, though.
Edit: Alternate solution using Sort+BinarySearch, as suggested by Kobi:
// Assuming your data is an ArrayList called "source"
source.Sort();
ArrayList dest = new ArrayList();
foreach (int i in source) if (dest.BinarySearch(i)<0) dest.Add(i);
public ArrayList RemoveDups ( ArrayList input )
{
ArrayList single_values = new ArrayList();
foreach( object item in input)
{
if( !single_values.Contains(item) )
{
single_values.Add(item);
}
}
return single_values;
}
Have you considered changing your ArrayList
to IEnumerable<int>
, or perhaps List<int>
?
ArrayList myAL = new ArrayList {8, 1, 1, 1, 2, 1 ,2, 3, 4, 5, 5, 5, 6 };
var myInts = (from i in myAL.Cast<int>()
orderby i
select i).Distinct();
foreach (int i in myInts)
{
Console.WriteLine(i);
}