Eliminate duplicates in string array

  • Thread starter Thread starter _eddie_
  • Start date Start date
E

_eddie_

I'm building an array of strings on the fly from a database. What is
the best method for eliminating duplicates? (I can do this before or
after the strings are added to the array)
 
An easy method

Hashtable strings = new Hashtable();
// Add strings to strings, using strings[stringName]
ArrayList stringArray = new ArrayList(strings.Keys);

You can stay with the string array or convert it down to an actual array.
 
_eddie_ said:
I'm building an array of strings on the fly from a database. What is
the best method for eliminating duplicates? (I can do this before or
after the strings are added to the array)

There's a number of ways to do it. Here's a simple one:

System.Collections.ArrayList a;
for ( ... )
{
if (!a.Contains(value))
a.Add(value);
}
return (string[]) a.ToArray(typeof(string));

Erik
 
An easy method

Hashtable strings = new Hashtable();
// Add strings to strings, using strings[stringName]
ArrayList stringArray = new ArrayList(strings.Keys);

You can stay with the string array or convert it down to an actual array.

Perfect. I had tried a hashtable, but I had more steps than necessary
(which may have accounted for some of the speed problems).

Thanks, Justin.
e
 
Back
Top