# Is there a way to add unique items to an array without doing a ton of comparisons?

Filed under:
|
|
##### loops

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)?

• #### Finding a problem in some task [closed]

as seen on Programmers - Search for 'Programmers'
Recently I competed in nation wide programming contest finals. Not unexpectedly all problems were algorithmic. I lost (40 points out of 600. Winner got ~300). I know why I lost very well - I don't know how to find actual problem in those obfuscated tasks which are life-blood of every competition.… >>> More

• #### Genetic algorithms

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm trying to implement a genetic algorithm that will calculate the minimum of the Rastrigin functon and I'm having some issues. I need to represent the chromosome as a binary string and as the Rastrigin's function takes a list of numbers as a parameter, how can decode the chromosome to a list of… >>> More

• #### understanding evaluation function

as seen on Programmers - Search for 'Programmers'
I am developing a chess program. And have made use of an alpha beta algorithm and a static evaluation function. I have successfully implemented both but I want to improve the evaluation function by automatically tuning the weights assigned to its features. At this point am totally confused about the… >>> More

• #### How to know whether to create a general system or to hack a solution

as seen on Programmers - Search for 'Programmers'
I'm new to coding , learning it since last year actually. One of my worst habits is the following: Often I'm trying to create a solution that is too big , too complex and doesn't achieve what needs to be achieved, when a hacky kludge can make the fit. One last example was the following (see paste… >>> More

• #### How to implement a genetic algorithm with distance, time, and cost

as seen on Programmers - Search for 'Programmers'
I want to make a solution to find the optimum route of school visit. For example, I want to visit 5 schools (A, B, C, D, E) in my city. Then I must find out what school I should visit first, then the second, then the third etc. with distance, time, and cost criteria. The problem is, I am confused… >>> More

• #### Array reverse and Array sort on One int array and store into another array using delgate

as seen on Stack Overflow - Search for 'Stack Overflow'
Array reverse and Array Sort on an int array and store into another array using a delegate. We have to accept the elements from the user >>> More

• #### LWJGL Voxel game, glDrawArrays

as seen on Game Development - Search for 'Game Development'
I've been learning about 3D for a couple days now. I managed to create a chunk (8x8x8). Add optimization so it only renders the active and visible blocks. Then I added so it only draws the faces which don't have a neighbor. Next what I found from online research was that it is better to use glDrawArrays… >>> More

• #### Sorting a Multidimensional array...

as seen on Stack Overflow - Search for 'Stack Overflow'
I need to sort an array that can look like this: \$array[4][0] = array('id' => 1, 'value' => 2); \$array[3][2] = array('id' => 0, 'value' => 3); \$array[4][1] = array('id' => 1, 'value' => 0); \$array[1][3] = array('id' => 2, 'value' => 1); \$array[1][1] = array('id' => 3, 'value'… >>> More

• #### why i add more insignificant code but cost less time

as seen on Stack Overflow - Search for 'Stack Overflow'
i write a method and when i add some insignificant code it works faster, like these : array[1]=array[1]; array[0]=array[0]; array[3]=array[3]; array[2]=array[2]; i use double t=System.currentTimeMillis(); at first to record the time. then call the method and use System.out.println(System.currentTimeMillis()-t);… >>> More

• #### Difference between array('Acl' => array('type' => 'requester')) and array('Acl' => 'requester') in C

as seen on Stack Overflow - Search for 'Stack Overflow'
I'm following the ACL tutorial for CakePHP 1.3 and I was wondering if there is a functional difference between declaring a behavior like this: var \$actsAs = array('Acl' => 'requester'); and like this: var \$actsAs = array('Acl' => array('type' => 'requester')); >>> More