Algorithms and Data Structures · Junior · Question 9 of 75
Why is set.has(x) usually faster than arr.includes(x) for large collections?
- 1A
Setkeeps its elements sorted and uses binary search - ✓Correct answer: A
Setis a hash-based structure, so a lookup is O(1) on average - 3A
Setstores only numbers, which compare faster
Explanation
Set (and Map) are implemented with hash tables: the value's hash points almost directly to where it is stored. Set is not sorted and can store values of any type.