Connecting Tech Pros Worldwide Help | Site Map

Eliminate duplicates in string array

  #1  
Old November 15th, 2005, 08:58 PM
_eddie_
Guest
 
Posts: n/a
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, 08:58 PM
Justin Rogers
Guest
 
Posts: n/a

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, 08:58 PM
Erik Frey
Guest
 
Posts: n/a

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, 09:02 PM
_eddie_
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
I want to know how I can do this using PHP & MySQL nse111 answers 8 August 5th, 2008 04:36 AM
Delete duplicates array (String) Mokita answers 4 March 20th, 2008 07:55 AM
Read-only string v4vijayakumar answers 24 June 22nd, 2006 04:55 PM
Encrypt and Decrypt in C# Gidi answers 8 November 17th, 2005 11:21 AM