Faster way to remove items from an array?


#1

Hello,
I wrote a script to remove items from an array,I am looking for a better solution to make it faster,anyone has a suggetion?Thanks!

Fn Remove_part_array arr a_start a_end=
    (
    	n=a_end-a_start+1
    	for i=1 to n do
    	(
    		deleteitem arr a_start
    	)
    	arr
    )
    a=#(1,2,3,4,5,6,7,8,9)
    --Goal b=#(1,2,3,4,9)

    b=Remove_part_array a 5 8
    --Remove from item[5] to item[8]
    print b
    ----------The result ----------
    1
    2
    3
    4
    9

#2

generally to collect items you need is faster than remove what you don’t need. but of course it depends on how many items you need remove and save


#3

If you simply want to remove the interval from the array of sorted integers that appear only once I’d suggest to use bitarrays instead


#4

OK,thank you all:ok_hand:


#5

You can also remove item by reverse iteration:

clearlistener()
TheArray = for i = 1 to 9 collect i
for i = 8 to 5 by -1 do deleteitem TheArray i
TheArray

#6

Thanks for help!