Bael 7599 (#16022)
* BAEL-7490 read write file in separate thread * Change the to try resources * Update the code to sync with article * BAEL-7599 find majority element * Added additional check on unit test * Update the index checking * Update the return type to Integer
This commit is contained in:
parent
eb3460d9ac
commit
a3ff5e0ec6
@ -0,0 +1,95 @@
|
||||
package com.baeldung.majorityelement;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
public class FindMajorityElement {
|
||||
|
||||
public static Integer findMajorityElementUsingForLoop(int[] nums) {
|
||||
int majorityThreshold = nums.length / 2;
|
||||
Integer majorityElement = null;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
int count = 0;
|
||||
for (int j = 0; j < nums.length; j++) {
|
||||
if (nums[i] == nums[j]) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count > majorityThreshold) {
|
||||
return majorityElement = nums[i];
|
||||
}
|
||||
}
|
||||
return majorityElement;
|
||||
}
|
||||
|
||||
public static Integer findMajorityElementUsingSorting(int[] nums) {
|
||||
Arrays.sort(nums);
|
||||
int majorityThreshold = nums.length / 2;
|
||||
int count = 0;
|
||||
for (int i = 0; i < nums.length; i++) {
|
||||
if (nums[i] == nums[majorityThreshold]) {
|
||||
count++;
|
||||
}
|
||||
|
||||
if (count > majorityThreshold) {
|
||||
return nums[majorityThreshold];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer findMajorityElementUsingHashMap(int[] nums) {
|
||||
Map<Integer, Integer> frequencyMap = new HashMap<>();
|
||||
|
||||
for (int num : nums) {
|
||||
frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
|
||||
}
|
||||
|
||||
int majorityThreshold = nums.length / 2;
|
||||
for (Map.Entry<Integer, Integer> entry : frequencyMap.entrySet()) {
|
||||
if (entry.getValue() > majorityThreshold) {
|
||||
return entry.getKey();
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public static Integer findMajorityElementUsingMooreVoting(int[] nums) {
|
||||
int majorityThreshold = nums.length / 2;
|
||||
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 (candidate == nums[i]) {
|
||||
count++;
|
||||
} else {
|
||||
count--;
|
||||
}
|
||||
|
||||
System.out.println("Iteration " + i + ": [candidate - " + candidate + ", count - " + count + ", element - " + nums[i] + "]");
|
||||
}
|
||||
|
||||
count = 0;
|
||||
for (int num : nums) {
|
||||
if (num == candidate) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count > majorityThreshold ? candidate : null;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
int[] nums = { 2, 3, 2, 4, 2, 5, 2 };
|
||||
Integer majorityElement = findMajorityElementUsingMooreVoting(nums);
|
||||
|
||||
if (majorityElement != null) {
|
||||
System.out.println("Majority element with maximum occurrences: " + majorityElement);
|
||||
} else {
|
||||
System.out.println("No majority element found");
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,64 @@
|
||||
package com.baeldung.majorityelement;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class FindMajorityElementUnitTest {
|
||||
|
||||
@Test
|
||||
void givenArrayWithMajorityElement_WhenUsingForLoop_ThenReturnFound() {
|
||||
int[] nums = { 2, 3, 2, 4, 2, 5, 2 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingForLoop(nums);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMajorityElement_WhenUsingForLoop_ThenNotFound() {
|
||||
int[] nums = { 2, 2, 3, 3 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingForLoop(nums);
|
||||
assertEquals(null, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithMajorityElement_WhenUsingSorting_ThenReturnFound() {
|
||||
int[] nums = { 2, 3, 2, 4, 2, 5, 2 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingSorting(nums);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenArrayWithoutMajorityElement_WhenUsingSorting_ThenNotFound() {
|
||||
int[] nums = { 2, 2, 3, 3 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingSorting(nums);
|
||||
assertEquals(null, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithMajorityElement_WhenUsingHashMap_ThenReturnFound() {
|
||||
int[] nums = { 2, 3, 2, 4, 2, 5, 2 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingHashMap(nums);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMajorityElement_WhenUsingHashMap_ThenNotFound() {
|
||||
int[] nums = { 2, 2, 3, 3 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingHashMap(nums);
|
||||
assertEquals(null, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithMajorityElement_WhenUsingMooreVoting_ThenReturnFound() {
|
||||
int[] nums = { 2, 3, 2, 4, 2, 5, 2 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingMooreVoting(nums);
|
||||
assertEquals(2, result);
|
||||
}
|
||||
|
||||
@Test
|
||||
void givenArrayWithoutMajorityElement_WhenUsingMooreVoting_ThenNotFound() {
|
||||
int[] nums = { 2, 2, 3, 3 };
|
||||
Integer result = FindMajorityElement.findMajorityElementUsingMooreVoting(nums);
|
||||
assertEquals(null, result);
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user