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

Get help using Construct 2

Post » Sat Apr 22, 2017 12:08 am

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?
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Sat Apr 22, 2017 1:18 am

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?
Mistakes were made.
B
52
S
26
G
110
Posts: 1,619
Reputation: 62,159

Post » Sat Apr 22, 2017 1:31 am

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
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Sat Apr 22, 2017 2:06 am

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
Mistakes were made.
B
52
S
26
G
110
Posts: 1,619
Reputation: 62,159

Post » Sat Apr 22, 2017 9:37 pm

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?
B
22
S
8
Posts: 203
Reputation: 2,059

Post » Mon Apr 24, 2017 1:42 am

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...
Mistakes were made.
B
52
S
26
G
110
Posts: 1,619
Reputation: 62,159

Post » Mon Apr 24, 2017 2:19 am

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
Mistakes were made.
B
52
S
26
G
110
Posts: 1,619
Reputation: 62,159

Post » Sun Apr 30, 2017 12:55 pm

Awesome, thank you so much ^-^
B
22
S
8
Posts: 203
Reputation: 2,059


Return to How do I....?

Who is online

Users browsing this forum: Artcadev, dop2000 and 34 guests