Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Majority Element in an Array (#1384)
- Loading branch information
1 parent
7afc471
commit 12dc189
Showing
5 changed files
with
291 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
A majority element in an array arr of size n is an | ||
element that appears more than n/2 times. | ||
Example: | ||
N = 9, arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Majority Element = 2 as count[2] = 5 which is greater | ||
than n/2 i.e 4 | ||
*/ | ||
|
||
#include<stdio.h> | ||
#define SIZE 1000007 | ||
|
||
int checkMajorityElement(int arr[], int N) | ||
{ | ||
int freq[SIZE] = {0}; | ||
// Computing frequency of each element | ||
for (int i = 0; i < N; i++) | ||
freq[arr[i]]++; | ||
for (int i = 0; i < SIZE; i++) | ||
{ | ||
if (freq[i] != 0 && freq[i] > (N / 2)) | ||
return i; | ||
} | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int N; | ||
printf("Enter size of array:\n"); | ||
scanf("%d", &N); | ||
int arr[N]; | ||
printf("Enter elements of array:\n"); | ||
for (int i = 0; i < N; i++) | ||
scanf("%d", &arr[i]); | ||
int ans = checkMajorityElement(arr, N); | ||
if (ans != -1) | ||
printf("Majority Element is: %d", ans); | ||
else | ||
printf("No majority element in array"); | ||
return 0; | ||
} | ||
|
||
/* | ||
Input: | ||
N = 9 | ||
arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Output: | ||
Majority Element is: 2 | ||
Input: | ||
N = 5 | ||
arr = {1, 2, 3, 4, 5} | ||
Output: | ||
No majority element in array | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
A majority element in an array arr of size n is an | ||
element that appears more than n/2 times. | ||
Example: | ||
N = 9, arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Majority Element = 2 as count[2] = 5 which is greater | ||
than n/2 i.e 4 | ||
*/ | ||
|
||
#include<bits/stdc++.h> | ||
using namespace std; | ||
|
||
int checkMajorityElement(int arr[], int N) | ||
{ | ||
map<int, int> mp; | ||
// Computing frequency of each element using Map | ||
for (int i = 0; i < N; i++) | ||
mp[arr[i]]++; | ||
map<int, int> :: iterator it; | ||
for (it = mp.begin(); it != mp.end(); it++) | ||
{ | ||
if (it -> second > (N / 2)) | ||
return it -> first; | ||
} | ||
return -1; | ||
} | ||
|
||
int main() | ||
{ | ||
int N; | ||
cout<<"Enter size of array:"<<endl; | ||
cin>>N; | ||
int arr[N]; | ||
cout<<"Enter elements of array:"<<endl; | ||
for (int i = 0; i < N; i++) | ||
cin>>arr[i]; | ||
int ans = checkMajorityElement(arr, N); | ||
if (ans != -1) | ||
cout<<"Majority Element is: "<<ans<<endl; | ||
else | ||
cout<<"No majority element in array"<<endl; | ||
return 0; | ||
} | ||
|
||
/* | ||
Input: | ||
N = 9 | ||
arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Output: | ||
Majority Element is: 2 | ||
Input: | ||
N = 5 | ||
arr = {1, 2, 3, 4, 5} | ||
Output: | ||
No majority element in array | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
A majority element in an array arr of size n is an | ||
element that appears more than n/2 times. | ||
Example: | ||
N = 9, arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Majority Element = 2 as count[2] = 5 which is greater | ||
than n/2 i.e 4 | ||
*/ | ||
|
||
package main | ||
import "fmt" | ||
|
||
func checkMajorityElement(arr[] int, N int) int { | ||
mp := make(map[int] int) | ||
// Computing frequency of each element | ||
for i := 0; i < N; i++ { | ||
mp[arr[i]]++ | ||
} | ||
|
||
for key, value := range mp { | ||
if value > (N / 2) { | ||
return key | ||
} | ||
} | ||
return -1 | ||
} | ||
|
||
func main() { | ||
var N int | ||
fmt.Printf("Enter size of array:\n") | ||
fmt.Scanf("%d", &N) | ||
arr := make([]int, N) | ||
fmt.Printf("Enter elements of array:\n") | ||
for i := 0; i < N; i++ { | ||
fmt.Scanf("%d", &arr[i]) | ||
} | ||
ans := checkMajorityElement(arr, N) | ||
if ans != -1 { | ||
fmt.Printf("Majority Element is: %d", ans) | ||
} else { | ||
fmt.Printf("No majority element in array") | ||
} | ||
} | ||
|
||
/* | ||
Input: | ||
N = 9 | ||
arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Output: | ||
Majority Element is: 2 | ||
Input: | ||
N = 5 | ||
arr = {1, 2, 3, 4, 5} | ||
Output: | ||
No majority element in array | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
/* | ||
A majority element in an array arr of size n is an | ||
element that appears more than n/2 times. | ||
Example: | ||
N = 9, arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Majority Element = 2 as count[2] = 5 which is greater | ||
than n/2 i.e 4 | ||
*/ | ||
|
||
import java.util.Scanner; | ||
import java.util.Map; | ||
import java.util.HashMap; | ||
|
||
class Majority_Element | ||
{ | ||
public static int checkMajorityElement(int arr[], int N) | ||
{ | ||
Map<Integer, Integer> mp = new HashMap<Integer, Integer>(); | ||
// Computing frequency of each element using Map | ||
for (int i = 0; i < N; i++) | ||
{ | ||
if (mp.containsKey(arr[i])) | ||
mp.put(arr[i], mp.get(arr[i]) + 1); | ||
else | ||
mp.put(arr[i], 1); | ||
} | ||
for (Map.Entry<Integer, Integer> entry : mp.entrySet()) | ||
{ | ||
if (entry.getValue() > (N / 2)) | ||
return entry.getKey(); | ||
} | ||
return -1; | ||
} | ||
|
||
public static void main(String args[]) | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
System.out.println("Enter size of array:"); | ||
int N = sc.nextInt(); | ||
int arr[] = new int[N]; | ||
System.out.println("Enter elements of array:"); | ||
for (int i = 0; i < N; i++) | ||
arr[i] = sc.nextInt(); | ||
int ans = checkMajorityElement(arr, N); | ||
if (ans != -1) | ||
System.out.println("Majority Element is: " + ans); | ||
else | ||
System.out.println("No majority element in array"); | ||
} | ||
} | ||
|
||
/* | ||
Input: | ||
N = 9 | ||
arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Output: | ||
Majority Element is: 2 | ||
Input: | ||
N = 5 | ||
arr = {1, 2, 3, 4, 5} | ||
Output: | ||
No majority element in array | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
''' | ||
A majority element in an array arr of size n is an | ||
element that appears more than n/2 times. | ||
Example: | ||
N = 9, arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Majority Element = 2 as count[2] = 5 which is greater | ||
than n/2 i.e 4 | ||
''' | ||
|
||
def checkMajorityElement(arr, N): | ||
mp = {} | ||
# Computing frequency of each element using Map | ||
for i in range(0, N): | ||
if arr[i] in mp.keys(): | ||
mp[arr[i]] += 1 | ||
else: | ||
mp[arr[i]] = 1 | ||
|
||
# iterating over map | ||
for key in mp: | ||
if mp[key] > (N / 2): | ||
return key | ||
return -1 | ||
|
||
print("Enter size of array:") | ||
N = int(input()) | ||
print("Enter elements of array:") | ||
arr = [int(x) for x in input().split(' ')] | ||
ans = checkMajorityElement(arr, N) | ||
if ans != -1: | ||
print("Majority Element is: %d" % ans) | ||
else: | ||
print("No majority element in array") | ||
|
||
''' | ||
Input: | ||
N = 9 | ||
arr = {1, 2, 2, 2, 4, 2, 2, 1, 5} | ||
Output: | ||
Majority Element is: 2 | ||
Input: | ||
N = 5 | ||
arr = {1, 2, 3, 4, 5} | ||
Output: | ||
No majority element in array | ||
''' |