Posted on by Kalkicode
Code Bit Logic

# Find the majority element using bit logic

The problem at hand is to find the majority element in a given array. A majority element is an element that appears more than n/2 times in an array of n elements, where n is the size of the array.

## Example

Let's consider an example to understand the problem better. Suppose we have an array arr[] = {1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7}. In this array, the number 7 appears 6 times out of the total 11 elements, which is more than n/2 (11/2 = 5). Hence, the majority element in this array is 7.

## Pseudocode

Before diving into the algorithm, let's summarize the approach in pseudocode:

``````findMajorityElement(arr[], n):
bits = 32
count = 0
x = 0

for i in range(0, bits):
for j in range(0, n):
if (arr[j] & (1 << i)) != 0:
count++

if count > (n / 2):
x += (1 << i)

count = 0

count = 0
for i in range(0, n):
if arr[i] == x:
count++

if count > n / 2:
print("Majority element is", x)
else:
print("No Majority element in this array")
``````

## Algorithm Explanation

1. The function `findMajorityElement` takes the array `arr[]` and its size `n` as input.
2. We use a variable `bits` to denote the number of bits in an integer (typically 32 for most systems).
3. We initialize a variable `count` to keep track of the number of active bits at a specific position (ith position) across all array elements.
4. We use a variable `x` to find the candidate majority element by setting the bits at positions where the count of set bits is greater than n/2.
5. We loop through each bit position (from 0 to `bits-1`) and count the number of set bits (1) at that position across all elements of the array.
6. If the count of set bits at the current position is greater than n/2, we set the corresponding bit of `x`.
7. After obtaining `x`, we count its frequency in the array to verify if it indeed appears more than n/2 times.
8. If `x` satisfies the majority condition, we print it as the majority element; otherwise, we print that there is no majority element.

## Code Solution

Here given code implementation process.

``````// C Program for
// Find the majority element using bit logic
#include <stdio.h>

void findMajorityElement(int arr[], int n)
{
// Auxiliary variable
int bits = 32;
int count = 0;
int x = 0;
// This loop are work on range (1...31) bit number
for (int i = 0; i < bits; ++i)
{
// Count active bits in ith position in every array element
for (int j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) != 0)
{
count++;
}
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
}
// Count frequency of x element
for (int i = 0; i < n; ++i)
{
if (arr[i] == x)
{
count++;
}
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
printf("\n Majority element is %d", x);
}
else
{
printf("\n No Majority element in this array ");
}
}
int main()
{
int arr[] = {
1 , 7 , 3 , 7 , 4 , 3 , 7 , 7 , 51 , 7 , 7
};
// Get the length of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
findMajorityElement(arr, n);
return 0;
}``````

#### Output

`` Majority element is 7``
``````// Java program for
// Find the majority element using bit logic
public class Searching
{
public void findMajorityElement(int[] arr, int n)
{
// Auxiliary variable
int bits = 32;
int count = 0;
int x = 0;
// This loop are work on range (1...31) bit number
for (int i = 0; i < bits; ++i)
{
// Count active bits in ith position in every array element
for (int j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) != 0)
{
count++;
}
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
}
// Count frequency of x element
for (int i = 0; i < n; ++i)
{
if (arr[i] == x)
{
count++;
}
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
System.out.print("\n Majority element is " + x);
}
else
{
System.out.print("\n No Majority element in this array ");
}
}
public static void main(String[] args)
{
int[] arr = {
1 , 7 , 3 , 7 , 4 , 3 , 7 , 7 , 51 , 7 , 7
};
// Get the length of array
int n = arr.length;
// Test
}
}``````

#### Output

`` Majority element is 7``
``````// Include header file
#include <iostream>
using namespace std;
// C++ program for
// Find the majority element using bit logic
class Searching
{
public: void findMajorityElement(int arr[], int n)
{
// Auxiliary variable
int bits = 32;
int count = 0;
int x = 0;
// This loop are work on range (1...31) bit number
for (int i = 0; i < bits; ++i)
{
// Count active bits in ith position in every array element
for (int j = 0; j < n; ++j)
{
if ((arr[j] &(1 << i)) != 0)
{
count++;
}
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
}
// Count frequency of x element
for (int i = 0; i < n; ++i)
{
if (arr[i] == x)
{
count++;
}
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
cout << "\n Majority element is " << x;
}
else
{
cout << "\n No Majority element in this array ";
}
}
};
int main()
{
int arr[] = {
1 , 7 , 3 , 7 , 4 , 3 , 7 , 7 , 51 , 7 , 7
};
// Get the length of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test
return 0;
}``````

#### Output

`` Majority element is 7``
``````// Include namespace system
using System;
// Csharp program for
// Find the majority element using bit logic
public class Searching
{
public void findMajorityElement(int[] arr, int n)
{
// Auxiliary variable
int bits = 32;
int count = 0;
int x = 0;
// This loop are work on range (1...31) bit number
for (int i = 0; i < bits; ++i)
{
// Count active bits in ith position in every array element
for (int j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) != 0)
{
count++;
}
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
}
// Count frequency of x element
for (int i = 0; i < n; ++i)
{
if (arr[i] == x)
{
count++;
}
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
Console.Write("\n Majority element is " + x);
}
else
{
Console.Write("\n No Majority element in this array ");
}
}
public static void Main(String[] args)
{
int[] arr = {
1 , 7 , 3 , 7 , 4 , 3 , 7 , 7 , 51 , 7 , 7
};
// Get the length of array
int n = arr.Length;
// Test
}
}``````

#### Output

`` Majority element is 7``
``````package main
import "fmt"
// Go program for
// Find the majority element using bit logic
type Searching struct {}
func getSearching() * Searching {
var me *Searching = &Searching {}
return me
}
func(this Searching) findMajorityElement(arr[] int, n int) {
// Auxiliary variable
var bits int = 32
var count int = 0
var x int = 0
// This loop are work on range (1...31) bit number
for i := 0 ; i < bits ; i++ {
// Count active bits in ith position in every array element
for j := 0 ; j < n ; j++ {
if (arr[j] & (1 << i)) != 0 {
count++
}
}
if count > (n / 2) {
// Add value of i-th bit
x += (1 << i)
}
count = 0
}
// Count frequency of x element
for i := 0 ; i < n ; i++ {
if arr[i] == x {
count++
}
}
if count > n / 2 {
// x appears more than n/2 times in the given array
fmt.Print("\n Majority element is ", x)
} else {
fmt.Print("\n No Majority element in this array ")
}
}
func main() {
var task * Searching = getSearching()
var arr = [] int {1 , 7 , 3 , 7 , 4 , 3 , 7 , 7 , 51 , 7 , 7}
// Get the length of array
var n int = len(arr)
// Test
}``````

#### Output

`` Majority element is 7``
``````<?php
// Php program for
// Find the majority element using bit logic
class Searching
{
public	function findMajorityElement(\$arr, \$n)
{
// Auxiliary variable
\$bits = 32;
\$count = 0;
\$x = 0;
// This loop are work on range (1...31) bit number
for (\$i = 0; \$i < \$bits; ++\$i)
{
// Count active bits in ith position in every array element
for (\$j = 0; \$j < \$n; ++\$j)
{
if ((\$arr[\$j] & (1 << \$i)) != 0)
{
\$count++;
}
}
if (\$count > ((int)(\$n / 2)))
{
// Add value of i-th bit
\$x += (1 << \$i);
}
\$count = 0;
}
// Count frequency of x element
for (\$i = 0; \$i < \$n; ++\$i)
{
if (\$arr[\$i] == \$x)
{
\$count++;
}
}
if (\$count > (int)(\$n / 2))
{
// x appears more than n/2 times in the given array
echo("\n Majority element is ".\$x);
}
else
{
echo("\n No Majority element in this array ");
}
}
}

function main()
{
\$arr = array(1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7);
// Get the length of array
\$n = count(\$arr);
// Test
}
main();``````

#### Output

`` Majority element is 7``
``````// Node JS program for
// Find the majority element using bit logic
class Searching
{
findMajorityElement(arr, n)
{
// Auxiliary variable
var bits = 32;
var count = 0;
var x = 0;
// This loop are work on range (1...31) bit number
for (var i = 0; i < bits; ++i)
{
// Count active bits in ith position in every array element
for (var j = 0; j < n; ++j)
{
if ((arr[j] & (1 << i)) != 0)
{
count++;
}
}
if (count > (parseInt(n / 2)))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
}
// Count frequency of x element
for (var i = 0; i < n; ++i)
{
if (arr[i] == x)
{
count++;
}
}
if (count > parseInt(n / 2))
{
// x appears more than n/2 times in the given array
process.stdout.write("\n Majority element is " + x);
}
else
{
process.stdout.write("\n No Majority element in this array ");
}
}
}

function main()
{
var arr = [1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7];
// Get the length of array
var n = arr.length;
// Test
}
main();``````

#### Output

`` Majority element is 7``
``````#  Python 3 program for
#  Find the majority element using bit logic
class Searching :
def findMajorityElement(self, arr, n) :
#  Auxiliary variable
bits = 32
count = 0
x = 0
i = 0
#  This loop are work on range (1...31) bit number
while (i < bits) :
j = 0
#  Count active bits in ith position in every list element
while (j < n) :
if ((arr[j] & (1 << i)) != 0) :
count += 1

j += 1

if (count > (int(n / 2))) :
#  Add value of i-th bit
x += (1 << i)

count = 0
i += 1

i = 0
#  Count frequency of x element
while (i < n) :
if (arr[i] == x) :
count += 1

i += 1

if (count > int(n / 2)) :
#  x appears more than n/2 times in the given list
print("\n Majority element is ", x, end = "")
else :
print("\n No Majority element in this array ", end = "")

def main() :
arr = [1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7]
#  Get the length of list
n = len(arr)
#  Test

if __name__ == "__main__": main()``````

#### Output

`` Majority element is  7``
``````#  Ruby program for
#  Find the majority element using bit logic
class Searching
def findMajorityElement(arr, n)
#  Auxiliary variable
bits = 32
count = 0
x = 0
i = 0
#  This loop are work on range (1...31) bit number
while (i < bits)
j = 0
#  Count active bits in ith position in every array element
while (j < n)
if ((arr[j] & (1 << i)) != 0)
count += 1
end

j += 1
end

if (count > (n / 2))
#  Add value of i-th bit
x += (1 << i)
end

count = 0
i += 1
end

i = 0
#  Count frequency of x element
while (i < n)
if (arr[i] == x)
count += 1
end

i += 1
end

if (count > n / 2)
#  x appears more than n/2 times in the given array
print("\n Majority element is ", x)
else

print("\n No Majority element in this array ")
end

end

end

def main()
arr = [1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7]
#  Get the length of array
n = arr.length
#  Test
end

main()``````

#### Output

`` Majority element is 7``
``````// Scala program for
// Find the majority element using bit logic
class Searching()
{
def findMajorityElement(arr: Array[Int], n: Int): Unit = {
// Auxiliary variable
var bits: Int = 32;
var count: Int = 0;
var x: Int = 0;
var i: Int = 0;
// This loop are work on range (1...31) bit number
while (i < bits)
{
var j: Int = 0;
// Count active bits in ith position in every array element
while (j < n)
{
if ((arr(j) & (1 << i)) != 0)
{
count += 1;
}
j += 1;
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
i += 1;
}
i = 0;
// Count frequency of x element
while (i < n)
{
if (arr(i) == x)
{
count += 1;
}
i += 1;
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
print("\n Majority element is " + x);
}
else
{
print("\n No Majority element in this array ");
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Searching = new Searching();
var arr: Array[Int] = Array(1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7);
// Get the length of array
var n: Int = arr.length;
// Test
}
}``````

#### Output

`` Majority element is 7``
``````import Foundation;
// Swift 4 program for
// Find the majority element using bit logic
class Searching
{
func findMajorityElement(_ arr: [Int], _ n: Int)
{
// Auxiliary variable
let bits: Int = 32;
var count: Int = 0;
var x: Int = 0;
var i: Int = 0;
// This loop are work on range (1...31) bit number
while (i < bits)
{
var j: Int = 0;
// Count active bits in ith position in every array element
while (j < n)
{
if ((arr[j] & (1 << i))  != 0)
{
count += 1;
}
j += 1;
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 << i);
}
count = 0;
i += 1;
}
i = 0;
// Count frequency of x element
while (i < n)
{
if (arr[i] == x)
{
count += 1;
}
i += 1;
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
print("\n Majority element is ",
x, terminator: "");
}
else
{
print("\n No Majority element in this array ",
terminator: "");
}
}
}
func main()
{
let arr: [Int] = [1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7];
// Get the length of array
let n: Int = arr.count;
// Test
}
main();``````

#### Output

`` Majority element is  7``
``````// Kotlin program for
// Find the majority element using bit logic
class Searching
{
fun findMajorityElement(arr: Array < Int > , n: Int): Unit
{
// Auxiliary variable
val bits: Int = 32;
var count: Int = 0;
var x: Int = 0;
var i: Int = 0;
// This loop are work on range (1...31) bit number
while (i < bits)
{
var j: Int = 0;
// Count active bits in ith position in every array element
while (j < n)
{
if ((arr[j] and(1 shl i)) != 0)
{
count += 1;
}
j += 1;
}
if (count > (n / 2))
{
// Add value of i-th bit
x += (1 shl i);
}
count = 0;
i += 1;
}
i = 0;
// Count frequency of x element
while (i < n)
{
if (arr[i] == x)
{
count += 1;
}
i += 1;
}
if (count > n / 2)
{
// x appears more than n/2 times in the given array
print("\n Majority element is " + x);
}
else
{
print("\n No Majority element in this array ");
}
}
}
fun main(args: Array < String > ): Unit
{
val arr: Array < Int > = arrayOf(1, 7, 3, 7, 4, 3, 7, 7, 51, 7, 7);
// Get the length of array
val n: Int = arr.count();
// Test
}``````

#### Output

`` Majority element is 7``

## Explanation of Result

After running the given code with the provided example array, the output is "Majority element is 7", which matches our expectations.

## Time Complexity

Let's analyze the time complexity of the algorithm. The outer loop runs `bits` times, which is a constant value (32 in this case). The inner loop runs `n` times. Hence, the time complexity of the algorithm is O(bits * n), which is equivalent to O(n). In most practical cases, bits is a small constant, making the algorithm linear in the size of the input array.

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

Categories
Relative Post