What you are wanting to do is best described using the
filter function. It turns a list into a new list where all members satisfy a predicate. However in most languages this allocates memory and then copies the values from the input array.
Another way to implement this functionality that reduces copying (and is closer to what you described) is used by R and older array programming languages. E.g.
array > 3 would return a list of indices that satisfy the test, and you can index an array with an array to get a array, so you could write:
array[array > 3]To get the elements of
array greater than 3.
So: Yes, this will work, but you are not directly making use of language level facilities to do it, even though you are using sets. I would suggest using an array of indices, not a set.