Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

majority element in an array #559

Merged
merged 1 commit into from
Oct 5, 2021
Merged
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@

// import java.util.Arrays;
import java.util.HashMap;

public class majority_Element {
public static void main(String[] args) {
int[] nums = { 2, 2, 1, 1, 1, 2, 2 };
int answer = optimal(nums);
System.out.println(answer);
}

// private static int brute(int[] nums) {
// int n = nums.length;
// for (int i = 0; i < n; i++) {
// int count = 0;
// int element = nums[i];
// for (int j = 0; j < n; j++) {
// if (i != j) {
// if (nums[j] == element) {
// count++;
// }
// }
// if (count > n / 2) {
// return element;
// }
// }
// }
// return -1;
// }

// private static int better1(int[] nums) {
// Arrays.sort(nums);
// if (nums.length <= 1)
// return nums[0];
// Arrays.sort(nums);
// int count = 1;
// for (int i = 0; i < nums.length - 1; i++) {
// if (nums[i] == nums[i + 1]) {
// count++;
// } else {
// count = 1;
// }
// if (count > nums.length / 2)
// return nums[i];
// }
// return -1;
// }

// private static int better2(int[] nums) {
// Arrays.sort(nums);
// return nums[nums.length / 2];
// }

private static int optimal(int[] nums) {
if (nums.length <= 1)
return nums[0];
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int count = map.get(nums[i]) + 1;
if (count > nums.length / 2)
return nums[i];
else
map.put(nums[i], count);

} else
map.put(nums[i], 1);
}
return -1;
}
}