Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Answer15.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// C++ program to find Majority
// element in an array
#include <bits/stdc++.h>
using namespace std;

// Function to find Majority element
// in an array
void findMajority(int arr[], int n)
{
int maxCount = 0;
int index = -1; // sentinels
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}

// update maxCount if count of
// current element is greater
if (count > maxCount) {
maxCount = count;
index = i;
}
}

// if maxCount is greater than n/2
// return the corresponding element
if (maxCount > n / 2)
cout << arr[index] << endl;

else
cout << "No Majority Element" << endl;
}

// Driver code
int main()
{
int arr[] = { 1, 1, 2, 1, 3, 5, 1 };
int n = sizeof(arr) / sizeof(arr[0]);

// Function calling
findMajority(arr, n);

return 0;
}
46 changes: 46 additions & 0 deletions Answer16.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// C++ program to find Majority
// element in an array
#include <bits/stdc++.h>
using namespace std;

// Function to find Majority element
// in an array
void findMajority(int arr[], int n)
{
int maxCount = 0;
int index = -1; // sentinels
for (int i = 0; i < n; i++) {
int count = 0;
for (int j = 0; j < n; j++) {
if (arr[i] == arr[j])
count++;
}

// update maxCount if count of
// current element is greater
if (count > maxCount) {
maxCount = count;
index = i;
}
}

// if maxCount is greater than n/3
// return the corresponding element
if (maxCount > n / 3)
cout << arr[index] << endl;

else
cout << "No Majority Element" << endl;
}

// Driver code
int main()
{
int arr[] = { 1, 1, 2, 1, 3, 5, 1 };
int n = sizeof(arr) / sizeof(arr[0]);

// Function calling
findMajority(arr, n);

return 0;
}