Connecting Tech Pros Worldwide Help | Site Map

Eliminate duplicates in string array

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 15th, 2005, 07:58 PM
_eddie_
Guest
 
Posts: n/a
Default Eliminate duplicates in string array

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)


  #2  
Old November 15th, 2005, 07:58 PM
Justin Rogers
Guest
 
Posts: n/a
Default 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]


  #3  
Old November 15th, 2005, 07:58 PM
Erik Frey
Guest
 
Posts: n/a
Default 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


  #4  
Old November 15th, 2005, 08:02 PM
_eddie_
Guest
 
Posts: n/a
Default 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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.