Skip to content

Commit 95c0971

Browse files
committed
Add majority element problem
1 parent 8a0fd72 commit 95c0971

File tree

5 files changed

+116
-0
lines changed

5 files changed

+116
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ List of Programs related to data structures and algorithms
5858
| 17 | Pascal's Triangle | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/17.pascalsTriangle/pascalsTriangle.md) | Easy | Array traversal and sequential sum |
5959
| 18 | Remove Element | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/18.removeElement/removeElement.md) | Easy | Array traversal |
6060
| 19 | Can place flowers | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/19.canPlaceFlowers/canPlaceFlowers.md) | Easy | Array traversal and comparison |
61+
| 20 | Majority Element | [Source](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.js) | [JavaScript](https://livecodes.io/?console=open&x=https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.js) | [Documentation](https://github.com/sudheerj/datastructures-algorithms/blob/master/src/javascript/algorithms/array/20.majorityElement/majorityElement.md) | Easy | Boyer Moore Voting algorithm |
6162

6263
### String
6364

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package java1.algorithms.array.majorityElement;
2+
3+
public class MajorityElement {
4+
private static int majorityElement(int[] nums){
5+
int count = 0;
6+
int candidate = 0;
7+
8+
for (int num : nums) {
9+
if(count == 0) {
10+
candidate = num;
11+
}
12+
13+
count += num == count ? 1 : -1;
14+
}
15+
16+
return candidate;
17+
}
18+
public static void main(String[] args) {
19+
int[] nums1 = new int[]{5,4,5,5};
20+
int[] nums2 = new int[]{3,4,3,5,3,3,1,3};
21+
System.out.println(majorityElement(nums1));
22+
System.out.println(majorityElement(nums2));
23+
}
24+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
**Description:**
2+
Given an integer array `nums` of size `n`, find the majority element from an array. The majority element is defined as the element that appears more than ⌊n / 2⌋ times.
3+
4+
**Note:** The majority element always exists in an array.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [5,4,5,5]
10+
Output: 5
11+
12+
Example 2:
13+
14+
Input: nums = [3,4,3,5,3,3,1,3]
15+
Output: 3
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of Boyer Moore Voting algorithm. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function(`majorityElement`) by accepting integer array(`nums`) to determine the majority element.
21+
22+
2. Initialize the counting variable(`count`) to determine number of occurances of an element. The default of majority element(`candidate`) is initialized to `0`.
23+
24+
3. Iterate over the array, and for each iteration compare the counting variable equals to 0 or not.
25+
1. If count value is equals to zero, update the candidate value with current element.
26+
27+
2. The counter value will be incremented by 1 if both candidate and current element are same, otherwise the counter is decremented by 1.
28+
29+
3. Since there is a gurantee of majority element, you don't need to verify whether the calculated element is majority element or not. i.e, majority element occurs more than half of array(`n/2`) times, where `n` is the length of given array.
30+
31+
4. After completion of iteration, return candidate value to represent majority element in the given number.
32+
33+
**Time and Space complexity:**
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
35+
36+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than counting variable.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
function majorityElement(nums){
2+
let count = 0;
3+
let candidate = null;
4+
5+
for (const num of nums) {
6+
if(count === 0) {
7+
candidate = num;
8+
}
9+
10+
count += num === candidate ? 1 : -1;
11+
}
12+
13+
return candidate;
14+
}
15+
16+
const nums1 = [5,4,5,5];
17+
const nums2 = [3,4,3,5,3,3,1,3];
18+
console.log(majorityElement(nums1));
19+
console.log(majorityElement(nums2));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
**Description:**
2+
Given an integer array `nums` of size `n`, find the majority element from an array. The majority element is defined as the element that appears more than ⌊n / 2⌋ times.
3+
4+
**Note:** The majority element always exists in an array.
5+
6+
### Examples
7+
Example 1:
8+
9+
Input: nums = [5,4,5,5]
10+
Output: 5
11+
12+
Example 2:
13+
14+
Input: nums = [3,4,3,5,3,3,1,3]
15+
Output: 3
16+
17+
**Algorithmic Steps**
18+
This problem is solved with the help of Boyer Moore Voting algorithm. The algorithmic approach can be summarized as follows:
19+
20+
1. Create a function(`majorityElement`) by accepting integer array(`nums`) to determine the majority element.
21+
22+
2. Initialize the counting variable(`count`) to determine number of occurances of an element. The default of majority element(`candidate`) is initialized to `null`.
23+
24+
3. Iterate over the array, and for each iteration compare the counting variable equals to 0 or not.
25+
1. If count value is equals to zero, update the candidate value with current element.
26+
27+
2. The counter value will be incremented by 1 if both candidate and current element are same, otherwise the counter is decremented by 1.
28+
29+
3. Since there is a gurantee of majority element, you don't need to verify whether the calculated element is majority element or not. i.e, majority element occurs more than half of array(`n/2`) times, where `n` is the length of given array.
30+
31+
4. After completion of iteration, return candidate value to represent majority element in the given number.
32+
33+
**Time and Space complexity:**
34+
This algorithm has a time complexity of `O(n)`, where `n` is the number of elements in an array. This is because we need to iterate over all the elements at most once.
35+
36+
It takes constant time complexity of `O(1)` due to no additional datastructure been used other than counting variable.

0 commit comments

Comments
 (0)