473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

best algorithm for getting distinct list

Bob
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

Nov 20 '05 #1
3 1249
On 2004-02-02, Bob <no*****@nospam.net> 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
Nov 20 '05 #2
Cor
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

Nov 20 '05 #3
Bob
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

Nov 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

303
by: mike420 | last post by:
In the context of LATEX, some Pythonista asked what the big successes of Lisp were. I think there were at least three *big* successes. a. orbitz.com web site uses Lisp for algorithms, etc. b....
1
by: rdsteph | last post by:
I am having a lot of fun using the pyGoogle module ( http://pygoogle.sourceforge.net/ ) that uses the Google API. It is about as easy to use as I can imagine, and it is a lot nicer than using my...
23
by: middletree | last post by:
I've seen posts here and elsewhere which read something along the lines of "PULLNG MY HAIR OUT!!!!!" or "HELLLLPPP!". Well, I know that kind of subject line isn't descriptive, but I sure can...
136
by: Matt Kruse | last post by:
http://www.JavascriptToolbox.com/bestpractices/ I started writing this up as a guide for some people who were looking for general tips on how to do things the 'right way' with Javascript. Their...
10
by: Kent | last post by:
Hi! I want to store data (of enemys in a game) as a linked list, each node will look something like the following: struct node { double x,y; // x and y position coordinates struct enemy...
8
by: Ben Fidge | last post by:
I have a requirement to compare two lists, generating a third which is a combination of both. With the first list as the reference list and the second as the test list, I want the each of the...
9
by: raylopez99 | last post by:
What's the best way of implementing a multi-node tree in C++? What I'm trying to do is traverse a tree of possible chess moves given an intial position (at the root of the tree). Since every...
51
by: Joerg Schoen | last post by:
Hi folks! Everyone knows how to sort arrays (e. g. quicksort, heapsort etc.) For linked lists, mergesort is the typical choice. While I was looking for a optimized implementation of mergesort...
6
by: kamsmartx | last post by:
I'm new to programming and need some help figuring out an algorithm. I need to design some kind of algorithm which will help us define capacity for one of our portfolios....here's the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.