229. Majority Element II LeetCode Solution
In this guide, you will get 229. Majority Element II LeetCode Solution with the best time and space complexity. The solution to Majority Element II problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Majority Element II solution in C++
- Majority Element II solution in Java
- Majority Element II solution in Python
- Additional Resources
Problem Statement of Majority Element II
Given an integer array of size n, find all elements that appear more than ⌊ n/3 ⌋ times.
Example 1:
Input: nums = [3,2,3]
Output: [3]
Example 2:
Input: nums = [1]
Output: [1]
Example 3:
Input: nums = [1,2]
Output: [1,2]
Constraints:
1 <= nums.length <= 5 * 104
-109 <= nums[i] <= 109
Follow up: Could you solve the problem in linear time and in O(1) space?
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(1)
229. Majority Element II LeetCode Solution in C++
class Solution {
public:
vector<int> majorityElement(vector<int>& nums) {
vector<int> ans;
int candidate1 = 0;
int candidate2 = 1; // any number different from candidate1
int countSoFar1 = 0; // the number of candidate1 so far
int countSoFar2 = 0; // the number of candidate2 so far
for (const int num : nums)
if (num == candidate1) {
++countSoFar1;
} else if (num == candidate2) {
++countSoFar2;
} else if (countSoFar1 == 0) { // Assign the new candidate.
candidate1 = num;
++countSoFar1;
} else if (countSoFar2 == 0) { // Assign the new candidate.
candidate2 = num;
++countSoFar2;
} else { // Meet a new number, so pair with the previous counts.
--countSoFar1;
--countSoFar2;
}
const int count1 = ranges::count(nums, candidate1);
const int count2 = ranges::count(nums, candidate2);
if (count1 > nums.size() / 3)
ans.push_back(candidate1);
if (count2 > nums.size() / 3)
ans.push_back(candidate2);
return ans;
}
};
/* code provided by PROGIEZ */
229. Majority Element II LeetCode Solution in Java
class Solution {
public List<Integer> majorityElement(int[] nums) {
List<Integer> ans = new ArrayList<>();
int candidate1 = 0;
int candidate2 = 1; // any number different from candidate1
int countSoFar1 = 0; // the number of candidate1 so far
int countSoFar2 = 0; // the number of candidate2 so far
for (final int num : nums)
if (num == candidate1) {
++countSoFar1;
} else if (num == candidate2) {
++countSoFar2;
} else if (countSoFar1 == 0) { // Assign the new candidate.
candidate1 = num;
++countSoFar1;
} else if (countSoFar2 == 0) { // Assign the new candidate.
candidate2 = num;
++countSoFar2;
} else { // Meet a new number, so pair with the previous counts.
--countSoFar1;
--countSoFar2;
}
int count1 = 0;
int count2 = 0;
for (final int num : nums)
if (num == candidate1)
++count1;
else if (num == candidate2)
++count2;
if (count1 > nums.length / 3)
ans.add(candidate1);
if (count2 > nums.length / 3)
ans.add(candidate2);
return ans;
}
}
// code provided by PROGIEZ
229. Majority Element II LeetCode Solution in Python
class Solution:
def majorityElement(self, nums: list[int]) -> list[int]:
ans1 = 0
ans2 = 1
count1 = 0
count2 = 0
for num in nums:
if num == ans1:
count1 += 1
elif num == ans2:
count2 += 1
elif count1 == 0:
ans1 = num
count1 = 1
elif count2 == 0:
ans2 = num
count2 = 1
else:
count1 -= 1
count2 -= 1
return [ans for ans in (ans1, ans2) if nums.count(ans) > len(nums) // 3]
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.