How do I delete numbers in an array that only appear once

0 favourites
  • 8 posts
From the Asset Store
Change the size and position of everything without calculating anything!
  • So I found a way to delete duplicates of a number from an array but how would I delete the numbers that only appear once and save all of the double, triples, etc?

  • How large is your number/value range, and how large is your array? The straight forward method will be pretty resource intensive if you have a lot of numbers and/or a large array to check.

    You might want to approach this from a different angle, depending on your situation. How is your array being generated in the first place?

  • My array is being generated by an equation that I made (and then sorted from lowest to highest). As of now the array is about 200x1x1

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • Ok if it's a sorted one dimension array that makes things a lot simpler.

    For each x,

    If array.curvalue = array.at(curx+1)

    Or

    If array.curvalue = array.at(curx-1)

    (Do nothing)

    Else

    Delete array index array.curx

  • I was having trouble getting that to function so I made this instead

    For each x

    Array.CurValue != Array.At(Array.CurX+1)

    Array.CurValue != Array.at(Array.CurX-1)

    Action: Delete Array.IndexOf(Array.CurValue) from X axis

    For some reason it's only working like half the time, what am I doing wrong?

  • It was trickier than I first thought.

    So it looks like deleting the index as you go doesn't work, because the loop index/current x doesn't match up anymore after you delete one.

    So I used a second array to "store" all the singles i found in the first index, then went back and deleted them after.

    https://www.dropbox.com/s/5r4ntrx41u6ia ... .capx?dl=0

    Unfortunately it doesn't work with 0 at either end of the array, because values that are outside of the array range are returned as 0s, so it thinks it is next to another 0.

    Edit: actually now that I think of it some more I feel like I've done this before another way. If IndexOf and LastIndexOf match, then there is only one of that value. I'll experiment a bit more...

  • Here is an updated one where you don't need a second array.

    It simply replaces all single values with a placeholder value "delete", and then afterwords loops back and gets rid of all indexes of "delete".

    https://www.dropbox.com/s/v4u712hdlvnns ... .capx?dl=0

  • Awesome, thank you so much ^-^

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)