Skip to content
Closed
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
55 changes: 55 additions & 0 deletions src/main/java/com/thealgorithms/others/MooreVoting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.util.Arrays;

public class MajorityElement {
public static int findMajorityElement(int[] nums) {
int candidate = nums[0];
int count = 1;

for (int i = 1; i < nums.length; i++) {
if (count == 0)
{
candidate = nums[i];
count = 1;
} else if (nums[i] == candidate)
{
count++;
} else
{
count--;

}
}

// Verify if candidate is the majority element
count = 0;
for (int num : nums)
{
if (num == candidate)
{
count++;
}
}

if (count > nums.length / 2)
{
return candidate;
} else
{
return -1; // No majority element found
}
}

public static void main(String[] args) {
int[] nums = {2, 1, 2, 1, 2, 2, 1};
int majorityElement = findMajorityElement(nums);

if (majorityElement != -1)
{
System.out.println("Majority element: " + majorityElement);
} else
{
System.out.println("No majority element found");

}
}
}