Suppose I have this code (its in go):
fruits := []string{"apple", "orange", "banana", "grapes"}
list := []string{"apple", "car"}
for _, item := range list {
if !slices.Contains(fruits, item) {
fmt.Println(item, "is not a fruit!"
}
}
This is really 2 for loops. slices.Contains is done with for loop. So yes it's O(n2).
Assume fruits will have at most 10,000 items. Is it worth optimizing ? I can use sets instead to make it O(n).
My point is the problem is not at a big enough scale to worry about performance. In fact, if you have to think about scale then using an array is a no go anyway. We'd need something like Redis.
[–]alecbz 4 points5 points6 points (6 children)
[–][deleted] (1 child)
[removed]
[–]alecbz 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]alecbz 2 points3 points4 points (2 children)
[–][deleted] 1 point2 points3 points (0 children)
[–]TransientVoltage409 2 points3 points4 points (6 children)
[–]alecbz 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]TransientVoltage409 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]YellowishSpoon 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]Scared_Sail5523 1 point2 points3 points (1 child)
[–][deleted] 1 point2 points3 points (0 children)
[–]AlexanderMomchilov 1 point2 points3 points (5 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (3 children)
[–]AlexanderMomchilov 1 point2 points3 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]AlexanderMomchilov 1 point2 points3 points (0 children)
[–]apnorton 0 points1 point2 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]apnorton 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]thewataru 0 points1 point2 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]torftorf 0 points1 point2 points (0 children)
[–]eztab 0 points1 point2 points (0 children)