Skip to content

Commit 94b6d86

Browse files
authored
Create 0169-majority-element.java
1 parent 5e4bf59 commit 94b6d86

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

Easy/0169-majority-element.java

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/**
2+
* 169. Majority Element
3+
* Difficulty: Easy
4+
* URL: https://leetcode.com/problems/majority-element/description/
5+
*
6+
* ---------------------
7+
* Approach 1: Brute Force (Nested Loops)
8+
*
9+
* Intuition:
10+
* - For each element, count how many times it appears in the array.
11+
* - If it appears more than n/2 times, return it.
12+
*
13+
* Time Complexity: O(n^2) — for each element, we scan the entire array
14+
* Space Complexity: O(1) — no extra data structures used
15+
*/
16+
class Solution {
17+
public int majorityElement(int[] nums) {
18+
for (int i = 0; i < nums.length; i++) {
19+
int count = 0;
20+
for (int j = 0; j < nums.length; j++) {
21+
if (nums[i] == nums[j]) count++;
22+
}
23+
if (count > nums.length / 2) return nums[i];
24+
}
25+
return -1;
26+
}
27+
}
28+
29+
/**
30+
* ---------------------
31+
* Approach 2: Sorting
32+
*
33+
* Intuition:
34+
* - After sorting, the majority element will always occupy the middle position.
35+
*
36+
* Time Complexity: O(n log n) — due to sorting
37+
* Space Complexity: O(1) or O(log n) depending on sorting implementation
38+
*/
39+
class Solution {
40+
public int majorityElement(int[] nums) {
41+
Arrays.sort(nums);
42+
return nums[nums.length / 2];
43+
}
44+
}
45+
46+
/**
47+
* ---------------------
48+
* Approach 3: HashMap Counting
49+
*
50+
* Intuition:
51+
* - Count occurrences of each element using a HashMap.
52+
* - The element with count > n/2 is the majority element.
53+
*
54+
* Time Complexity: O(n) — iterate and count in one pass
55+
* Space Complexity: O(n) — to store counts in HashMap
56+
*/
57+
class Solution {
58+
public int majorityElement(int[] nums) {
59+
HashMap<Integer, Integer> map = new HashMap<>();
60+
for (int ele : nums) {
61+
map.put(ele, map.getOrDefault(ele, 0) + 1);
62+
}
63+
int level = nums.length / 2;
64+
for (int key : map.keySet()) {
65+
if (map.get(key) > level) return key;
66+
}
67+
return -1;
68+
}
69+
}
70+
71+
/**
72+
* ---------------------
73+
* Approach 4: Boyer-Moore Voting Algorithm (Optimized)
74+
*
75+
* Intuition:
76+
* - The majority element appears more than n/2 times in the array.
77+
* - Keep a candidate (ans) and a count.
78+
* - If count becomes 0, select a new candidate.
79+
* - If the current element equals the candidate, increment count, otherwise decrement.
80+
* - The majority element will remain as the final candidate.
81+
*
82+
* Time Complexity: O(n) — single scan
83+
* Space Complexity: O(1) — constant space
84+
*/
85+
class Solution {
86+
public int majorityElement(int[] nums) {
87+
int ans = 0, count = 0;
88+
for (int i = 0; i < nums.length; i++) {
89+
if (count == 0)
90+
ans = nums[i];
91+
if (nums[i] == ans) {
92+
count++;
93+
} else {
94+
count--;
95+
}
96+
}
97+
return ans;
98+
}
99+
}

0 commit comments

Comments
 (0)