Connecting Tech Pros Worldwide Help | Site Map

Eliminate duplicates in string array

_eddie_
Guest
 
Posts: n/a
#1: Nov 15 '05
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)

Justin Rogers
Guest
 
Posts: n/a
#2: Nov 15 '05

re: Eliminate duplicates in string 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.


--
Justin Rogers
DigiTec Web Consultants, LLC.
Blog: http://weblogs.asp.net/justin_rogers

"_eddie_" <_nomail_@_nospam_.com> wrote in message
news:5deu101t0pqn08pouiecrh5rn5usksiihr@4ax.com...[color=blue]
> 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)
>[/color]


Erik Frey
Guest
 
Posts: n/a
#3: Nov 15 '05

re: Eliminate duplicates in string array


"_eddie_" <_nomail_@_nospam_.com> wrote in message
news:5deu101t0pqn08pouiecrh5rn5usksiihr@4ax.com...[color=blue]
> 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)[/color]

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


_eddie_
Guest
 
Posts: n/a
#4: Nov 15 '05

re: Eliminate duplicates in string array


On Mon, 2 Feb 2004 22:28:51 -0800, "Justin Rogers"
<Justin@games4dotnet.com> wrote:
[color=blue]
>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.[/color]

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
Closed Thread