I will be receiving strings one by one from a framework, I need to hold them in some container and delete some of them later. Now I have 2 options :-
So personally I would prefer second option. Is that correct choice? Do we have any better way?
If you need to access and delete strings by value, then a map[string]struct{}
would give you better performance provided the number of strings is large enough and there are no duplicates. If there are duplicates and when you delete you have to delete only one, then a map[string]int
would work, with the value being the number of times the string appears. If the number of strings are not large, then a container/list
might work better than a slice because you can delete strings from it in constant time. A slice would outperform others only for small sizes, for all practical cases, deletion would require copying part of the slice.