169 Majority Element
Link to the problem: https://leetcode.com/problems/majority-element
Solution 1
My intuition was simple - i use a dictionary (hash map) and just count occurences. I can also compare the maximum number of occurences in place, so I implemented.
Code C#
Complexity
- Time O(n)
- Space O(n)
Solution 2
Later I discovered (in Solution section), that there is a much simpler solution, that relates on the Moore’s Voting Algorithm. Believe it or not, you can just work with a simple counter variable. Since the major element will appeear more than n/2 times, it will eventually be captured by this algorithm.
What helped me to grasp the idea better - image you have a sorted array containing [1,1, 1, 1, 1, 2, 2, 3, 3]. In this case you will get the count
variable equal to 1 and the majorElement
will be 1. Sweet 🥹!
Code C#
Complexity
- Time O(n)
- Space O(1)