More Rust Code Interview Questions ep.7 majority element in an array

preview_player
Показать описание
FOLLOW ME:

In this video we look at finding the majoirty element in an array :)
Рекомендации по теме
Комментарии
Автор

Can't you just sort the array and take the middle element? In sorted array, the majority element is guaranteed to overlap with the middle. Though I would also probably used Hashmap to keep counts. That solution is O(n), while sorting is O(nlogn) *.

* I know radix sort exists, but it internally does pretty much what the hashmap solution does, except it also wastes time putting all the elements in order.

KohuGaly
visit shbcf.ru