Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions 25. Divide and Conquer/Majority_Element.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
public class Solution {
public int majorityElement(int[] num) {
int major = num[0], count = 1;
for (int i = 1; i < num.length; i++) {
if (count == 0) {
count++;
major = num[i];
} else if (major == num[i]) {
count++;
} else {
count--;
}
}
return major;
}

public static void main(String[] args) {
// Create an instance of the Solution class
Solution solution = new Solution();

// Example input array
int[] nums = {3, 2, 3};

// Call the majorityElement method and print the result
System.out.println("Majority Element: " + solution.majorityElement(nums)); // Output: 3
}
}