From 12dc189fe5b5f7330a5c5bcde16b23c3d1931d30 Mon Sep 17 00:00:00 2001 From: Shreya Singh Date: Thu, 23 May 2019 18:52:56 +0530 Subject: [PATCH] Majority Element in an Array (#1384) --- Majority_Element/Majority_Element.c | 58 ++++++++++++++++++++++ Majority_Element/Majority_Element.cpp | 59 +++++++++++++++++++++++ Majority_Element/Majority_Element.go | 59 +++++++++++++++++++++++ Majority_Element/Majority_Element.java | 66 ++++++++++++++++++++++++++ Majority_Element/Majority_Element.py | 49 +++++++++++++++++++ 5 files changed, 291 insertions(+) create mode 100644 Majority_Element/Majority_Element.c create mode 100644 Majority_Element/Majority_Element.cpp create mode 100644 Majority_Element/Majority_Element.go create mode 100644 Majority_Element/Majority_Element.java create mode 100644 Majority_Element/Majority_Element.py diff --git a/Majority_Element/Majority_Element.c b/Majority_Element/Majority_Element.c new file mode 100644 index 0000000000..be1a0a57d2 --- /dev/null +++ b/Majority_Element/Majority_Element.c @@ -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 +#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 +*/ diff --git a/Majority_Element/Majority_Element.cpp b/Majority_Element/Majority_Element.cpp new file mode 100644 index 0000000000..e3779febe3 --- /dev/null +++ b/Majority_Element/Majority_Element.cpp @@ -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 +using namespace std; + +int checkMajorityElement(int arr[], int N) +{ + map mp; + // Computing frequency of each element using Map + for (int i = 0; i < N; i++) + mp[arr[i]]++; + map :: 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:"<>N; + int arr[N]; + cout<<"Enter elements of array:"<>arr[i]; + int ans = checkMajorityElement(arr, N); + if (ans != -1) + cout<<"Majority Element is: "< (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 +*/ diff --git a/Majority_Element/Majority_Element.java b/Majority_Element/Majority_Element.java new file mode 100644 index 0000000000..fee912e9fa --- /dev/null +++ b/Majority_Element/Majority_Element.java @@ -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 mp = new HashMap(); + // 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 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 +*/ diff --git a/Majority_Element/Majority_Element.py b/Majority_Element/Majority_Element.py new file mode 100644 index 0000000000..a07865173b --- /dev/null +++ b/Majority_Element/Majority_Element.py @@ -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 +'''