top | item 27177366

(no title)

emehrkay | 4 years ago

I cant wait for that slices package. If I have to write this one more time im going to have a breakdown

    func inSlice(needle someType, haystack []someType) bool{}

discuss

order

SergeAx|4 years ago

I would use hashmap and get O(1) instead of O(N).

omginternets|4 years ago

Linear search through a slice is almost always going to be faster than a map operation, because N is almost always small and slices have great cache locality.

If your slice has a million entries, it's definitely a different story.

jniedrauer|4 years ago

Using a map might not be faster as others have pointed out, but it's often more readable. The core library does this in several places. A map[someType]bool makes for nice, readable code:

    if haystack[needle] {

grogenaut|4 years ago

You know that many hash functions are more expensive than iterating 1k worth of items that are contiguous in an array to find a matching item.

inertiatic|4 years ago

You know, inserting N elements into a hashmap is O(N).