Please note, this is a STATIC archive of website www.javatpoint.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
Javatpoint Logo
Javatpoint Logo

Majority Element In an Array in Java

It is a very interesting problem frequently asked in interviews of top IT companies like Google, Amazon, TCS, Accenture, etc. By solving the problem, one wants to check the logical ability, critical thinking, and problem-solving skill of the interviewee. So, in this section, we are going to find majority element in an array in Java with different approaches and logic. Also, we will create Java programs for the same.

Problem Statement

An array containing integers is given. Our task is to find the majority of elements present in the input array.

Majority Element

A majority element is an element whose number of occurrences is greater than half of the size of the input array.

Example 1:

Input:

Int arr[] = {5, 1, 1, 1, 1, 1, 4, 9, 1, 0, 1, -2}

Output: 1

Explanation: The count of occurrence of element 1 is 7, which is greater than half of the size of the input array, which is (12 / 2) = 6.

Example 2:

Input:

Int arr[] = {47, 8, 1, 6, 3, 6, 90, 52, 78, 47, 47, 47}

Output: No majority element exists in the input array.

Explanation: There is no element whose count of occurrence is more than half of the size of the input array.

Naive Approach

In this approach, we will take two nested loops. The outer loop will pick an element from the input array (from left to right). The inner loop will count the occurrence of the element picked by the outer loop. If the count is greater than half of the size of the input array, then we have our answer. If not, then consider the next iteration and so on. If the outer loop terminates, then it means all the elements of the input array have been exhausted, and the majority element is not found.

FileName: MajorityEle.java

Output:

For the input array.
5 1 1 1 1 1 4 9 1 0 1 2 
The majority element is: 1


For the input array.
47 8 1 6 3 6 90 52 78 47 47 47 
The majority element is not found.

Complexity Analysis: The program is using nested loops. Therefore, the time complexity of the program is O(n2), where n is the total number of elements present in the input array. The time complexity of the program is O(1), as the program is not using any extra space.

Approach: Using Binary Search Tree

In this approach, we will insert the elements of the input array one by one and if the element is already present in the Binary Search Tree, then increase its count. At any stage, if the count of any node becomes greater than half of the size of the input array, then we have our answer.

Algorithm

Observe the following algorithm.

Step 1: Construct a BST (Binary search tree); if the same element is entered again in the BST, then the count of the occurrence of the node is increased.

Step 2: Traverse the input array and put the element in the BST.

Step 3: If the maximum occurrence of any of the nodes is more than the half of the size of the input array, then do an in order traversal to find the node with an occurrence count of more than the half

Step 4: Else, print the appropriate message for the no majority element.

FileName: MajorityEle1.java

Output:

For the input array.
5 1 1 1 1 1 4 9 1 0 1 2 
The majority element is: 1


For the input array.
47 8 1 6 3 6 90 52 78 47 47 47 
The majority element is not found.

Complexity Analysis: The time complexity of the program is the same as the previous program. The space complexity of the program is O(n), where n is the total number of elements present in the input array. The space complexity is due to the construction of the Binary Search Tree for storing the nodes.

Approach: Using Sorting

Sorting will group the elements of the same value together. After sorting, all one has to do is take the count of the elements of the same value and check whether it is more than half of the size of the input array or not. The following program illustrates the same.

FileName: MajorityEle2.java

Output:

For the input array.
5 1 1 1 1 1 4 9 1 0 1 2 
The majority element is: 1


For the input array.
47 8 1 6 3 6 90 52 78 47 47 47 
The majority element is not found.

Complexity Analysis: Since the program is using sorting, the time complexity of the program is O(n * log(n)), where n is the total number of elements present in the input array. The space complexity of the program is constant, i.e., O(1).

Approach: Using HashMap

The approach is quite straightforward. Observe the following steps.

Step 1: Count the frequency of occurrence of the array elements.

Step 2: Store the array element and its occurrence count as the key-value pair in a hash map.

Step 3: Iterate over the hashmap and check if the value of the frequency count of the element is greater than half of the size of the input array or not.

Step 4: Display the output.

Look at the following program.

FileName: MajorityEle3.java

Output:

For the input array.
5 1 1 1 1 1 4 9 1 0 1 2 
The majority element is: 1


For the input array.
47 8 1 6 3 6 90 52 78 47 47 47 
The majority element is not found.

Complexity Analysis: Since the program is using only one loop, the time complexity of the program is O(n). The program is also using a hash map. Thus, making the space complexity of the program O(n), where n is the total number of elements present in the input array.

Approach: Using Moore's Voting Technique

Observe the following steps.

Step 1: Iterate over the elements to maintain the count of the majority element and the index, which is majIndex in our case.

Step 2: If the next element has the same value as the current element, then increase the count by 1.

Step 3: If the next element does not have the same value as the current element, then decrease the count by 1.

Step 4: If the count becomes 0, then update the majIndex by assigning the index of the current element and assigning the value 1 to the count.

Step 5: Pick the element pointed by majIndex, and find the count of occurrence of the element.

Step 6: If the count is greater than half of the size of the input array, then the element pointed by the majIndex is the majority element; otherwise, the majority element does not exist in the input array.

Look at the following program.

FileName: MajorityEle4.java

Output:

For the input array.
5 1 1 1 1 1 4 9 1 0 1 2 
The majority element is: 1


For the input array.
47 8 1 6 3 6 90 52 78 47 47 47 
The majority element is not found.

Complexity Analysis: The time complexity of the program is the same as the previous program. As the program is not using any data structure, the space complexity of the program is O(1).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA