169. Majority Element Easy
1/**
2 * [169] Majority Element
3 *
4 * Given an array nums of size n, return the majority element.
5 * The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array.
6 *
7 * Example 1:
8 * Input: nums = [3,2,3]
9 * Output: 3
10 * Example 2:
11 * Input: nums = [2,2,1,1,1,2,2]
12 * Output: 2
13 *
14 * Constraints:
15 *
16 * n == nums.length
17 * 1 <= n <= 5 * 10^4
18 * -10^9 <= nums[i] <= 10^9
19 *
20 *
21 * Follow-up: Could you solve the problem in linear time and in O(1) space?
22 */
23pub struct Solution {}
24
25// problem: https://leetcode.com/problems/majority-element/
26// discuss: https://leetcode.com/problems/majority-element/discuss/?currentPage=1&orderBy=most_votes&query=
27
28// submission codes start here
29
30impl Solution {
31 pub fn majority_element(nums: Vec<i32>) -> i32 {
32 0
33 }
34}
35
36// submission codes end
37
38#[cfg(test)]
39mod tests {
40 use super::*;
41
42 #[test]
43 fn test_169() {
44 }
45}
46
Back
© 2025 bowen.ge All Rights Reserved.