PC Review


Reply
Thread Tools Rate Thread

best algorithm for getting distinct list

 
 
Bob
Guest
Posts: n/a
 
      2nd Feb 2004
It's not especially important, but I always like to know the best way of doing
things for when I encounter a case where performance becomes a factor... say I
have a string array and I want to get a distinct list, that is, get a list
without duplicates. In SQL this is easy because it's done for you. In code, what
would be the best practice? Array.Sort(stringarray) followed by n comparisons
collecting on the changes? Or perhaps adding each string as a key to a
collection or hashtable and just letting duplicates refusal do the work? I have
always wondered, are there circumstances when catching errors in quantity like
this degrade performance? Is it common, avoided?

Bob

 
Reply With Quote
 
 
 
 
Tom Shelton
Guest
Posts: n/a
 
      2nd Feb 2004
On 2004-02-02, Bob <(E-Mail Removed)> wrote:
> It's not especially important, but I always like to know the best way of doing
> things for when I encounter a case where performance becomes a factor... say I
> have a string array and I want to get a distinct list, that is, get a list
> without duplicates. In SQL this is easy because it's done for you. In code, what
> would be the best practice? Array.Sort(stringarray) followed by n comparisons
> collecting on the changes? Or perhaps adding each string as a key to a
> collection or hashtable and just letting duplicates refusal do the work? I have
> always wondered, are there circumstances when catching errors in quantity like
> this degrade performance? Is it common, avoided?
>
> Bob


Catching exceptions is always expensive and should be avoided where
possible. Personally, I would use the hashtable approach, but I would
probably check if it existed first using the hashtable contains
method... The check would probably be less expensive then catching an
exception in terms of performance.

--
Tom Shelton [MVP]
Powered By Gentoo Linux 1.4
Well, you know, no matter where you go, there you are.
-- Buckaroo Banzai
 
Reply With Quote
 
Cor
Guest
Posts: n/a
 
      2nd Feb 2004
Hi Bob,

I think just as you said,
Array.sort
Extra Arraylist
Loop 1 time through the sorted array from 0 to array.length
Add the first from the arry to the arraylist and every time the item is
another than the previous
Would go very fast I think.

For me are exceptions only for if you cannot manage the exception, let say a
file which is incomplete readed from disk.

And for no other things, have you seen how long it takes when there is an
exception in your debugger?

I hope this helps,

Cor

> It's not especially important, but I always like to know the best way of

doing
> things for when I encounter a case where performance becomes a factor...

say I
> have a string array and I want to get a distinct list, that is, get a list
> without duplicates. In SQL this is easy because it's done for you. In

code, what
> would be the best practice? Array.Sort(stringarray) followed by n

comparisons
> collecting on the changes? Or perhaps adding each string as a key to a
> collection or hashtable and just letting duplicates refusal do the work? I

have
> always wondered, are there circumstances when catching errors in quantity

like
> this degrade performance? Is it common, avoided?
>
> Bob
>



 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      2nd Feb 2004
I sort of knew I shouldn't be lazy and let exception handling do the work, but I
wasn't sure. Thanks for the answers.

Bob

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Algorithm to combine identical items in a list William Microsoft Excel Programming 1 8th Jan 2010 11:57 AM
Unique/Distinct Dropdown List Shawn Microsoft Excel Worksheet Functions 10 29th May 2009 03:47 AM
Distinct list formula summergs Microsoft Excel Misc 5 16th Aug 2006 06:12 AM
HELP - Custom DRW Drop list using DISTINCT Mettá Microsoft Frontpage 1 19th Mar 2004 07:31 PM
Looking for List comparison algorithm Ben Fidge Microsoft C# .NET 8 4th Nov 2003 10:16 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:52 AM.