I'm looking for a function/way like list.count()
(count the occurrences of a list element in a list), but with a better time complexity. Is there a way?
Asked
Active
Viewed 102 times
0

martineau
- 119,623
- 25
- 170
- 301

QWERASDFYXCV
- 245
- 1
- 6
- 15
-
1I'm curious: how do you suggest to implement it in less than O(n)? – DeepSpace Oct 18 '18 at 18:40
-
1What makes you think that the time complexity of `list.count` is suboptimal? – Aran-Fey Oct 18 '18 at 18:40
-
Related: https://stackoverflow.com/questions/44812468/what-is-the-time-complexity-of-python-lists-count-function – DeepSpace Oct 18 '18 at 18:42
-
It's not possible to better than **O(n)** because every element in the list must be checked. – martineau Oct 18 '18 at 19:05