Is there a way to add unique items to an array without doing a ton of comparisons?
Posted
by
hydroparadise
on Programmers
See other posts from Programmers
or by hydroparadise
Published on 2012-11-01T15:02:09Z
Indexed on
2012/11/01
17:15 UTC
Read the original article
Hit count: 349
Please bare with me, I want this to be as language agnostic as possible becuase of the languages I am working with (One of which is a language called PowerOn). However, most languanges support for loops and arrays.
Say I have the following list in an aray:
0x 0 Foo
1x 1 Bar
2x 0 Widget
3x 1 Whatsit
4x 0 Foo
5x 1 Bar
Anything with a 1 should be uniqely added to another array with the following result:
0x 1 Bar
1x 1 Whatsit
Keep in mind this is a very elementry example. In reality, I am dealing with 10's of thousands of elements on the old list. Here is what I have so far.
Pseudo Code:
For each element in oldlist
For each element in newlist
Compare
If values oldlist.element equals newlist.element, break new list loop
If reached end of newlist with with nothing equal from oldlist, add value from old list to new list
End
End
Is there a better way of doing this? Algorithmicly, is there any room for improvement? And as a bonus qeustion, what is the O notation for this type of algorithm (if there is one)?
© Programmers or respective owner