❔ ✅ Hey is there a way to check if something is in a list in a certain range from the list ?
Like for example i got a list of [2, 1,5, 6,3, 4, 1] and i want to see if there is a five in the list from position 4- 6 without using a for loop ?
18 Replies
You could do
array[3..7].Contains(5)
thinker227#5176
REPL Result: Success
Result: bool
Compile: 570.448ms | Execution: 54.167ms | React with ❌ to remove this embed.
thx that was exactly what i was looking for ^^
np
$close
Use the
/close
command to mark a forum thread as answeredthat copied that part of the array, use
AsSpan
first
arr.AsSpan()[3 .. 6].Contains(4)
well first, they said list
I don't think lists support slicing
that is correct
but you can get the span of a list
collectionsmarshal time
CollectionsMarshal.AsSpan
or something like that
or use linq, it also doesn't make copies.Skip().SkipLast()
or .Skip().Take()
but linq could be even worse for small lists
in terms of memory
its a small list and this is used quite frequently so i dont know if a for loop is also a good solution bc of how small the list are (at most like 20 items but normally around 4- 9).
there is nothing better than a for loop for lists
literally impossible to get any better
the very best way is to use
CollectionsMarshal.AsSpan
, pin the resulting span, and work on that pointer
unless the type is managed
then you can't pinWas this issue resolved? If so, run
/close
- otherwise I will mark this as stale and this post will be archived until there is new activity.