forked from LeetCode-in-TypeScript/LeetCode-in-TypeScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsolution.ts
More file actions
31 lines (29 loc) · 861 Bytes
/
Copy pathsolution.ts
File metadata and controls
31 lines (29 loc) · 861 Bytes
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
// #Easy #Top_100_Liked_Questions #Top_Interview_Questions #Array #Hash_Table #Sorting #Counting
// #Divide_and_Conquer #Data_Structure_II_Day_1_Array #Udemy_Famous_Algorithm
// #Big_O_Time_O(n)_Space_O(1) #2023_10_09_Time_50_ms_(96.16%)_Space_46.1_MB_(46.90%)
function majorityElement(arr: number[]): number {
let count = 1
let majority = arr[0]
// For Potential Majority Element
for (let i = 1; i < arr.length; i++) {
if (arr[i] === majority) {
count++
} else if (count > 1) {
count--
} else {
majority = arr[i]
}
}
// For Confirmation
count = 0
for (let j of arr) {
if (j === majority) {
count++
}
}
if (count >= Math.floor(arr.length / 2) + 1) {
return majority
}
return -1
}
export { majorityElement }