-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmajority-element.js
More file actions
91 lines (73 loc) · 1.96 KB
/
majority-element.js
File metadata and controls
91 lines (73 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/**
* Problem: Majority Element
* Link: https://leetcode.com/problems/majority-element/
* Difficulty: Easy
*
* Given an array nums of size n, return the majority element.
* The majority element is the element that appears more than ⌊n / 2⌋ times.
*
* Example:
* Input: nums = [3,2,3]
* Output: 3
*
* Time Complexity: O(n)
* Space Complexity: O(1)
*/
// JavaScript Solution - Boyer-Moore Voting Algorithm
function majorityElement(nums) {
let candidate = null;
let count = 0;
// Find candidate
for (const num of nums) {
if (count === 0) {
candidate = num;
}
count += num === candidate ? 1 : -1;
}
return candidate;
}
// Alternative: HashMap approach
function majorityElementHashMap(nums) {
const counts = new Map();
const majority = Math.floor(nums.length / 2);
for (const num of nums) {
counts.set(num, (counts.get(num) || 0) + 1);
if (counts.get(num) > majority) {
return num;
}
}
}
// Test cases
console.log(majorityElement([3, 2, 3])); // 3
console.log(majorityElement([2, 2, 1, 1, 1, 2, 2])); // 2
console.log(majorityElement([1])); // 1
module.exports = majorityElement;
/* Python Solution (commented):
def majority_element(nums: list[int]) -> int:
"""
Find majority element using Boyer-Moore Voting Algorithm
Args:
nums: Array of integers
Returns:
The majority element
Time Complexity: O(n)
Space Complexity: O(1)
"""
candidate = None
count = 0
# Find candidate
for num in nums:
if count == 0:
candidate = num
count += 1 if num == candidate else -1
return candidate
# Alternative: HashMap approach
def majority_element_hashmap(nums: list[int]) -> int:
from collections import Counter
counts = Counter(nums)
return max(counts.keys(), key=counts.get)
# Test cases
print(majority_element([3,2,3])) # 3
print(majority_element([2,2,1,1,1,2,2])) # 2
print(majority_element([1])) # 1
*/