//create a test arraylist of integers
int[] arr = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 9};
ArrayList arrList = new ArrayList(arr);
//use a hashtable to create a unique list
Hashtable ht = new Hashtable();
foreach (int item in arrList) {
//set a key in the hashtable for our arraylist value - leaving the hashtable value empty
ht.Item[item] = null;
}
//now grab the keys from that hashtable into another arraylist
ArrayList distincArray = new ArrayList(ht.Keys);
Mike Banavige
Marked as answer by jazzybinary on Jul 05, 2008 01:59 PM
Was using the code and i had to convert it to VB so here it is and thanks a lot!
Dim arr As
Integer() = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 9}
Dim arrList
As
New ArrayList(arr)
'use a hashtable to create a unique list
Dim ht
As
New Hashtable()
For
Each item
As
Integer
In arrList
'set a key in the hashtable for our arraylist value - leaving the hashtable value empty
ht.Item(item) =
Nothing
Next
'now grab the keys from that hashtable into another arraylist
Dim distincArray
As
New ArrayList(ht.Keys)
For i
As
Integer = 0
To distincArray.Count - 1
Response.Write(distincArray(i) & "<br />")
Next
jazzybinary
Member
78 Points
83 Posts
distinct values from arraylist
Jul 05, 2008 01:21 PM|LINK
How can I receive distinct values from a huge arraylist?
Note: I can not use another array as a look up table cos I dont know the actual values.
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: distinct values from arraylist
Jul 05, 2008 01:38 PM|LINK
something like this should do it:
//create a test arraylist of integers int[] arr = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 9}; ArrayList arrList = new ArrayList(arr); //use a hashtable to create a unique list Hashtable ht = new Hashtable(); foreach (int item in arrList) { //set a key in the hashtable for our arraylist value - leaving the hashtable value empty ht.Item[item] = null; } //now grab the keys from that hashtable into another arraylist ArrayList distincArray = new ArrayList(ht.Keys);jazzybinary
Member
78 Points
83 Posts
Re: distinct values from arraylist
Jul 05, 2008 01:48 PM|LINK
thx mbanavige but it keep giving me 'System.Collections.Hashtable' does not contain a definition for 'Item'
error. How can I fix it?
jazzybinary
Member
78 Points
83 Posts
Re: distinct values from arraylist
Jul 05, 2008 01:59 PM|LINK
ht[item]
Thx man u are the star!!
mbanavige
All-Star
134961 Points
15421 Posts
ASPInsiders
Moderator
MVP
Re: distinct values from arraylist
Jul 05, 2008 02:03 PM|LINK
great. looks like the Item property doesnt translate well to c# although it exists for vb.
glad you got it working :-)
rjcox
Contributor
7064 Points
1444 Posts
Re: distinct values from arraylist
Jul 05, 2008 09:46 PM|LINK
The Item property is the default indexer, so is just accessed as property[id] in C# (and C# can't directly use non-default parametrised properties).
Just another little difference between C# and VB.
Paul Linton
Star
13403 Points
2531 Posts
Re: distinct values from arraylist
Jul 06, 2008 06:01 AM|LINK
var UniqueValues = nonUnique.Distinct().ToArray();
cheers
Gabriel82
Member
29 Points
12 Posts
Re: distinct values from arraylist
May 10, 2009 08:04 PM|LINK
Was using the code and i had to convert it to VB so here it is and thanks a lot!
Dim arr As Integer() = {1, 2, 3, 3, 3, 4, 4, 5, 5, 6, 7, 7, 7, 8, 8, 9, 9} Dim arrList As New ArrayList(arr) 'use a hashtable to create a unique list Dim ht As New Hashtable() For Each item As Integer In arrList 'set a key in the hashtable for our arraylist value - leaving the hashtable value emptyht.Item(item) =
Nothing Next 'now grab the keys from that hashtable into another arraylist Dim distincArray As New ArrayList(ht.Keys) For i As Integer = 0 To distincArray.Count - 1 Response.Write(distincArray(i) & "<br />") Next