Posted on by Kalkicode
Code Array

Check for majority element in a sorted array

Here given code implementation process.

``````/*
C program for
Check for majority element in a sorted array
*/
#include <stdio.h>

// Print array element
void printArray(int arr[], int n)
{
for (int i = 0; i < n; i++)
{
printf(" %d ", arr[i]);
}
printf("\n");
}
void isMajorityElement(int arr[], int n, int x)
{
printArray(arr, n);
// Result indicator
int result = 0;
// Count the number of majority element
int count = n / 2;
for (int i = 0; i < n && result == 0; ++i)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
}
if (result == 1)
{
printf(" Yes majority element is %d\n", x);
}
else
{
printf(" No majority element is not %d\n", x);
}
}
int main()
{
// Sorted array element
int arr[] = {
1 , 3 , 3 , 3 , 7 , 7 , 7 , 7 , 7 , 7 , 51
};
// Get n of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test A
isMajorityElement(arr, n, 3);
// Test B
isMajorityElement(arr, n, 7);
return 0;
}``````

Output

`````` 1  3  3  3  7  7  7  7  7  7  51
No majority element is not 3
1  3  3  3  7  7  7  7  7  7  51
Yes majority element is 7``````
``````/*
Java program for
Check for majority element in a sorted array
*/
public class Majority
{
// Print array element
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
System.out.print(" " + arr[i]);
}
System.out.print("\n");
}
public void isMajorityElement(int[] arr, int n, int x)
{
this.printArray(arr, n);
// Result indicator
int result = 0;
// Count the number of majority element
int count = n / 2;
for (int i = 0; i < n && result == 0; ++i)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
}
if (result == 1)
{
System.out.println(" Yes majority element is " + x );
}
else
{
System.out.println(" No majority element is not " + x );
}
}
public static void main(String[] args)
{
// Sorted array element
int[] arr = {
1 , 3 , 3 , 3 , 7 , 7 , 7 , 7 , 7 , 7 , 51
};
// Get n of array
int n = arr.length;
// Test A
// Test B
}
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````// Include header file
#include <iostream>
using namespace std;
/*
C++ program for
Check for majority element in a sorted array
*/
class Majority
{
public:
// Print array element
void printArray(int arr[], int n)
{
for (int i = 0; i < n; ++i)
{
cout << " " << arr[i];
}
cout << "\n";
}
void isMajorityElement(int arr[], int n, int x)
{
this->printArray(arr, n);
// Result indicator
int result = 0;
// Count the number of majority element
int count = n / 2;
for (int i = 0; i < n && result == 0; ++i)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
}
if (result == 1)
{
cout << " Yes majority element is " << x << endl;
}
else
{
cout << " No majority element is not " << x << endl;
}
}
};
int main()
{
// Sorted array element
int arr[] = {
1 , 3 , 3 , 3 , 7 , 7 , 7 , 7 , 7 , 7 , 51
};
// Get n of array
int n = sizeof(arr) / sizeof(arr[0]);
// Test A
// Test B
return 0;
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````// Include namespace system
using System;
/*
Csharp program for
Check for majority element in a sorted array
*/
public class Majority
{
// Print array element
public void printArray(int[] arr, int n)
{
for (int i = 0; i < n; ++i)
{
Console.Write(" " + arr[i]);
}
Console.Write("\n");
}
public void isMajorityElement(int[] arr, int n, int x)
{
this.printArray(arr, n);
// Result indicator
int result = 0;
// Count the number of majority element
int count = n / 2;
for (int i = 0; i < n && result == 0; ++i)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
}
if (result == 1)
{
Console.WriteLine(" Yes majority element is " + x);
}
else
{
Console.WriteLine(" No majority element is not " + x);
}
}
public static void Main(String[] args)
{
// Sorted array element
int[] arr = {
1 , 3 , 3 , 3 , 7 , 7 , 7 , 7 , 7 , 7 , 51
};
// Get n of array
int n = arr.Length;
// Test A
// Test B
}
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````package main
import "fmt"
/*
Go program for
Check for majority element in a sorted array
*/

// Print array element
func printArray(arr[] int, n int) {
for i := 0 ; i < n ; i++ {
fmt.Print(" ", arr[i])
}
fmt.Print("\n")
}
func isMajorityElement(arr[] int, n int, x int) {
printArray(arr, n)
// Result indicator
var result int = 0
// Count the number of majority element
var count int = n / 2
for i := 0 ; i < n && result == 0 ; i++ {
if arr[i] == x && arr[i + count] == x {
// i+count is more than half element
// And current element is equal to given value x
result = 1
}
}
if result == 1 {
fmt.Println(" Yes majority element is ", x)
} else {
fmt.Println(" No majority element is not ", x)
}
}
func main() {

// Sorted array element
var arr = [] int {1 , 3 , 3 , 3 , 7 , 7 , 7 , 7 , 7 , 7 , 51 }
// Get n of array
var n int = len(arr)
// Test A
isMajorityElement(arr, n, 3)
// Test B
isMajorityElement(arr, n, 7)
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````<?php
/*
Php program for
Check for majority element in a sorted array
*/
class Majority
{
// Print array element
public	function printArray(\$arr, \$n)
{
for (\$i = 0; \$i < \$n; ++\$i)
{
echo(" ".\$arr[\$i]);
}
echo("\n");
}
public	function isMajorityElement(\$arr, \$n, \$x)
{
\$this->printArray(\$arr, \$n);
// Result indicator
\$result = 0;
// Count the number of majority element
\$count = (int)(\$n / 2);
for (\$i = 0; \$i < \$n && \$result == 0; ++\$i)
{
if (\$arr[\$i] == \$x && \$arr[\$i + \$count] == \$x)
{
// i+count is more than half element
// And current element is equal to given value x
\$result = 1;
}
}
if (\$result == 1)
{
echo(" Yes majority element is ".\$x."\n");
}
else
{
echo(" No majority element is not ".\$x."\n");
}
}
}

function main()
{
// Sorted array element
\$arr = array(1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51);
// Get n of array
\$n = count(\$arr);
// Test A
// Test B
}
main();``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````/*
Node JS program for
Check for majority element in a sorted array
*/
class Majority
{
// Print array element
printArray(arr, n)
{
for (var i = 0; i < n; ++i)
{
process.stdout.write(" " + arr[i]);
}
process.stdout.write("\n");
}
isMajorityElement(arr, n, x)
{
this.printArray(arr, n);
// Result indicator
var result = 0;
// Count the number of majority element
var count = parseInt(n / 2);
for (var i = 0; i < n && result == 0; ++i)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
}
if (result == 1)
{
console.log(" Yes majority element is " + x);
}
else
{
console.log(" No majority element is not " + x);
}
}
}

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

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````#    Python 3 program for
#    Check for majority element in a sorted array
class Majority :
#  Print list element
def printArray(self, arr, n) :
i = 0
while (i < n) :
print(" ", arr[i], end = "")
i += 1

print(end = "\n")

def isMajorityElement(self, arr, n, x) :
self.printArray(arr, n)
#  Result indicator
result = 0
#  Count the number of majority element
count = int(n / 2)
i = 0
while (i < n and result == 0) :
if (arr[i] == x and arr[i + count] == x) :
#  i+count is more than half element
#  And current element is equal to given value x
result = 1

i += 1

if (result == 1) :
print(" Yes majority element is ", x)
else :
print(" No majority element is not ", x)

def main() :
#  Sorted list element
arr = [1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51]
#  Get n of list
n = len(arr)
#  Test A
#  Test B

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

Output

``````  1  3  3  3  7  7  7  7  7  7  51
No majority element is not  3
1  3  3  3  7  7  7  7  7  7  51
Yes majority element is  7``````
``````#    Ruby program for
#    Check for majority element in a sorted array
class Majority
#  Print array element
def printArray(arr, n)
i = 0
while (i < n)
print(" ", arr[i])
i += 1
end

print("\n")
end

def isMajorityElement(arr, n, x)
self.printArray(arr, n)
#  Result indicator
result = 0
#  Count the number of majority element
count = n / 2
i = 0
while (i < n && result == 0)
if (arr[i] == x && arr[i + count] == x)
#  i+count is more than half element
#  And current element is equal to given value x
result = 1
end

i += 1
end

if (result == 1)
print(" Yes majority element is ", x, "\n")
else

print(" No majority element is not ", x, "\n")
end

end

end

def main()
#  Sorted array element
arr = [1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51]
#  Get n of array
n = arr.length
#  Test A
#  Test B
end

main()``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7
``````
``````/*
Scala program for
Check for majority element in a sorted array
*/
class Majority()
{
// Print array element
def printArray(arr: Array[Int], n: Int): Unit = {
var i: Int = 0;
while (i < n)
{
print(" " + arr(i));
i += 1;
}
print("\n");
}
def isMajorityElement(arr: Array[Int], n: Int, x: Int): Unit = {
this.printArray(arr, n);
// Result indicator
var result: Int = 0;
// Count the number of majority element
var count: Int = n / 2;
var i: Int = 0;
while (i < n && result == 0)
{
if (arr(i) == x && arr(i + count) == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
i += 1;
}
if (result == 1)
{
println(" Yes majority element is " + x);
}
else
{
println(" No majority element is not " + x);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Majority = new Majority();
// Sorted array element
var arr: Array[Int] = Array(1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51);
// Get n of array
var n: Int = arr.length;
// Test A
// Test B
}
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````
``````import Foundation;
/*
Swift 4 program for
Check for majority element in a sorted array
*/
class Majority
{
// Print array element
func printArray(_ arr: [Int], _ n: Int)
{
var i: Int = 0;
while (i < n)
{
print(" ", arr[i], terminator: "");
i += 1;
}
print(terminator: "\n");
}
func isMajorityElement(_ arr: [Int], _ n: Int, _ x: Int)
{
self.printArray(arr, n);
// Result indicator
var result: Int = 0;
// Count the number of majority element
let count: Int = n / 2;
var i: Int = 0;
while (i < n && result == 0)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
i += 1;
}
if (result == 1)
{
print(" Yes majority element is ", x);
}
else
{
print(" No majority element is not ", x);
}
}
}
func main()
{
// Sorted array element
let arr: [Int] = [1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51];
// Get n of array
let n: Int = arr.count;
// Test A
// Test B
}
main();``````

Output

``````  1  3  3  3  7  7  7  7  7  7  51
No majority element is not  3
1  3  3  3  7  7  7  7  7  7  51
Yes majority element is  7``````
``````/*
Kotlin program for
Check for majority element in a sorted array
*/
class Majority
{
// Print array element
fun printArray(arr: Array < Int > , n: Int): Unit
{
var i: Int = 0;
while (i < n)
{
print(" " + arr[i]);
i += 1;
}
print("\n");
}
fun isMajorityElement(arr: Array < Int > , n: Int, x: Int): Unit
{
this.printArray(arr, n);
// Result indicator
var result: Int = 0;
// Count the number of majority element
val count: Int = n / 2;
var i: Int = 0;
while (i < n && result == 0)
{
if (arr[i] == x && arr[i + count] == x)
{
// i+count is more than half element
// And current element is equal to given value x
result = 1;
}
i += 1;
}
if (result == 1)
{
println(" Yes majority element is " + x);
}
else
{
println(" No majority element is not " + x);
}
}
}
fun main(args: Array < String > ): Unit
{
// Sorted array element
val arr: Array < Int > = arrayOf(1, 3, 3, 3, 7, 7, 7, 7, 7, 7, 51);
// Get n of array
val n: Int = arr.count();
// Test A
// Test B
}``````

Output

`````` 1 3 3 3 7 7 7 7 7 7 51
No majority element is not 3
1 3 3 3 7 7 7 7 7 7 51
Yes majority element is 7``````

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