• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GameMaker [SOLVED] Remove Duplicates from ds_list

Phil Strahl

Member
Hey, I am just wondering if there's an easy way to go through a ds_list and remove duplicate entries other than picking a candidate entry and going through the whole list looking for matches.
 

chamaeleon

Member
Hey, I am just wondering if there's an easy way to go through a ds_list and remove duplicate entries other than picking a candidate entry and going through the whole list looking for matches.
Some unknowns... Is it a short list or a long list? Is it sorted? Does removal need to preserve order?
 
M

Marcus12321

Guest
If you sort it, then you just need to check each member against the next one in the list. is x== x+1, etc...
 
C

Carbiner

Guest
What I've done in the past is this little snippet, which checks if there already is a duplicate in the list and only adds the entry if there isn't. I don't know how fast it is in comparison to other methods, and it may not be applicable in your case, anyways. My data doesn't need to be sorted, either, so that my introduce complications.

Code:
var val = numb; // Or however you get the data
if(!ds_list_find_index(list,val))
    {
    ds_list_add(list,val); // Add it only if it doesn't exist in the list already.
    }
 

Phil Strahl

Member
If you sort it, then you just need to check each member against the next one in the list. is x== x+1, etc...
What I've done in the past is this little snippet, which checks if there already is a duplicate in the list and only adds the entry if there isn't. I don't know how fast it is in comparison to other methods, and it may not be applicable in your case, anyways. My data doesn't need to be sorted, either, so that my introduce complications.

Code:
var val = numb; // Or however you get the data
if(!ds_list_find_index(list,val))
    {
    ds_list_add(list,val); // Add it only if it doesn't exist in the list already.
    }
Thank you all for the replies! Yep, that could work! I just need to bench all three approaches (the two here and my own) to see which performs best! :)
 

chamaeleon

Member
Thank you all for the replies! Yep, that could work! I just need to bench all three approaches (the two here and my own) to see which performs best! :)
A naive approach for a list of size 10 should be able to do about 100,000 iterations of removing duplicates in a second.
Edit: About 10 seconds for 1,000,000 iterations for a VM build and around 4 seconds for a YYC build.
 
Top