Is the implementation in HashSet.ElementAt O(1) and if not, what is it?
Is the implementation in HashSet.ElementAt O(1) and if not, what is it?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No, it’s O(n). All of the extension methods on
IEnumerable<T>are, by necessity O(n) (because the only thing thatIEnumerable<T>can do is … enumerate). Although, as pointed out in the comments, they do attempt to cast to an interface that can implement the operation faster (for example,ElementAtwill try to cast toIList<T>in order to implement an O(1) operation). Not that that helps in the case ofHashSet<T>which doesn’t implementIList<T>anyway.For
HashSet<T>the concept of “ElementAt” doesn’t really make sense anyway, because there is no “ordering” as such. You’re basically just getting a random element.