[thelist] Looping Psuedocode

Simon Willison cs1spw at bath.ac.uk
Thu Aug 28 10:52:26 CDT 2003


Rob Smith wrote:

> Hi,
> 
> I've struggled with this for quite some time now and still haven't found the
> best approach to this. Basically I want to see some Psuedocode that solves:
> 
>  from ::   to ::
>   Item A    Item A
>   Item A    Item B
>   Item A    Item C
>   Item B
>   Item B
>   Item B
>   Item B
>   Item C
>   Item C

The answer to this very much depends on what technology you are using - 
PHP for example has a built in function to do this. As pseudo code 
(actually Python which is close enough) here's how I would do it:

fromArray = ['a', 'a', 'a', 'b', 'b', 'b', 'c', 'c']
toArray = [] # An empty array

for item in fromArray:
     if item not in toArray:
         toArray.append(item)

Donning my computer scientist hat for a moment (algorithm complexity 
isn't a strong point of mine) I'd say that has at most quadratic 
complexity, which is pretty poor. Off the top of my head though I can't 
think of a better algorithm. For small data sets it should work fine though.

Cheers,

Simon Willison
http://simon.incutio.com/




More information about the thelist mailing list