Given an array arr[] and two elements x and y, return the element that occurs more frequently. If both elements have the same frequency, return the smaller one.
Examples:
Input: arr[] = [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5], x = 4, y = 5
Output: 4
Explanation: Frequency of 4 is 4 and frequency of 5 is 1. Since 4 > 1, return 4.Input: arr[] = [1, 2, 3, 4, 5, 6, 7, 8], x = 1, y = 7
Output: 1
Explanation: Frequency of 1 is 1 and frequency of 7 is 1. Since both have the same frequency and 1 < 7, return 1.
Table of Content
[Naive Approach] Count Frequencies Separately - O(n) Time and O(1) Space
The idea is to traverse the array separately for x and y to count their frequencies.
Then, compare both frequencies and return the element with the greater frequency.
Working of Approach:
- Initialize countX and countY as 0.
- Traverse the array once to count occurrences of x.
- Traverse the array again to count occurrences of y.
- Compare countX and countY.
- Return the smaller element if both frequencies are equal.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int moreFrequent(vector<int> &arr, int x, int y)
{
int countX = 0, countY = 0;
// Count frequency of x
for (int val : arr)
{
if (val == x)
countX++;
}
// Count frequency of y
for (int val : arr)
{
if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller element
return min(x, y);
}
int main()
{
vector<int> arr = {1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5};
int x = 4, y = 5;
cout << moreFrequent(arr, x, y) << endl;
return 0;
}
import java.util.*;
class GFG {
public int moreFrequent(List<Integer> arr, int x, int y)
{
int countX = 0, countY = 0;
// Count frequency of x
for (int val : arr) {
if (val == x)
countX++;
}
// Count frequency of y
for (int val : arr) {
if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller
// element
return Math.min(x, y);
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList(1, 1, 2, 2, 3, 3,
4, 4, 4, 4, 5);
int x = 4, y = 5;
GFG obj = new GFG();
System.out.println(obj.moreFrequent(arr, x, y));
}
}
def moreFrequent(arr, x, y):
countX = arr.count(x)
countY = arr.count(y)
# Return the element with greater frequency
if countX > countY:
return x
elif countY > countX:
return y
# If frequencies are equal, return the smaller element
return min(x, y)
if __name__ == '__main__':
arr = [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5]
x = 4
y = 5
print(moreFrequent(arr, x, y))
using System;
using System.Collections.Generic;
class GFG {
public int moreFrequent(List<int> arr, int x, int y)
{
int countX = 0, countY = 0;
// Count frequency of x
foreach(int val in arr)
{
if (val == x)
countX++;
}
// Count frequency of y
foreach(int val in arr)
{
if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller
// element
return Math.Min(x, y);
}
public static void Main(string[] args)
{
List<int> arr = new List<int>{ 1, 1, 2, 2, 3, 3,
4, 4, 4, 4, 5 };
int x = 4, y = 5;
GFG obj = new GFG();
Console.WriteLine(obj.moreFrequent(arr, x, y));
}
}
function moreFrequent(arr, x, y)
{
let countX = 0, countY = 0;
// Count frequency of x
for (let val of arr) {
if (val === x)
countX++;
}
// Count frequency of y
for (let val of arr) {
if (val === y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller element
return Math.min(x, y);
}
// Driver Code
let arr = [ 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5 ];
let x = 4, y = 5;
console.log(moreFrequent(arr, x, y));
Output
4
[Expected Approach] Count Both Frequencies in One Traversal - O(n) Time and O(1) Space
The idea is to traverse the array only once and count the occurrences of both x and y simultaneously.
After counting, compare their frequencies and return the more frequent element.
Working of Approach:
- Initialize countX and countY as 0.
- Traverse every element of the array once.
- Increment countX when the element is equal to x.
- Increment countY when the element is equal to y.
- Compare the frequencies and return the required element.
Let us understand with an example:
Input: arr[] = [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5], x = 4, y = 5
- Initialize countX = 0 and countY = 0 for x = 4 and y = 5.
- Traverse the array and count occurrences of both elements.
- The frequency of 4 is 4, so countX = 4.
- The frequency of 5 is 1, so countY = 1.
- Since countX > countY, the function returns 4.
#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;
int moreFrequent(vector<int> &arr, int x, int y)
{
int countX = 0, countY = 0;
// Count the frequencies of x and y
for (int val : arr)
{
if (val == x)
countX++;
else if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller element
return min(x, y);
}
int main()
{
vector<int> arr = {1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5};
int x = 4, y = 5;
cout << moreFrequent(arr, x, y) << endl;
return 0;
}
import java.util.*;
class GFG {
public int moreFrequent(List<Integer> arr, int x, int y)
{
int countX = 0, countY = 0;
// Count the frequencies of x and y
for (int val : arr) {
if (val == x)
countX++;
else if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller
// element
return Math.min(x, y);
}
public static void main(String[] args)
{
List<Integer> arr = Arrays.asList(1, 1, 2, 2, 3, 3,
4, 4, 4, 4, 5);
int x = 4, y = 5;
GFG obj = new GFG();
System.out.println(obj.moreFrequent(arr, x, y));
}
}
def moreFrequent(arr, x, y):
countX = 0
countY = 0
# Count the frequencies of x and y
for val in arr:
if val == x:
countX += 1
elif val == y:
countY += 1
# Return the element with greater frequency
if countX > countY:
return x
elif countY > countX:
return y
# If frequencies are equal, return the smaller element
return min(x, y)
if __name__ == '__main__':
arr = [1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5]
x = 4
y = 5
print(moreFrequent(arr, x, y))
using System;
using System.Collections.Generic;
class GFG {
public int moreFrequent(List<int> arr, int x, int y)
{
int countX = 0, countY = 0;
// Count the frequencies of x and y
foreach(int val in arr)
{
if (val == x)
countX++;
else if (val == y)
countY++;
}
// Return the element with greater frequency
if (countX > countY)
return x;
else if (countY > countX)
return y;
// If frequencies are equal, return the smaller
// element
return Math.Min(x, y);
}
public static void Main(string[] args)
{
List<int> arr = new List<int>{ 1, 1, 2, 2, 3, 3,
4, 4, 4, 4, 5 };
int x = 4, y = 5;
GFG obj = new GFG();
Console.WriteLine(obj.moreFrequent(arr, x, y));
}
}
function moreFrequent(arr, x, y)
{
let countX = 0, countY = 0;
// Count the frequencies of x and y
for (let val of arr) {
if (val === x) {
countX++;
}
else if (val === y) {
countY++;
}
}
// Return the element with greater frequency
if (countX > countY) {
return x;
}
else if (countY > countX) {
return y;
}
// If frequencies are equal, return the smaller element
return Math.min(x, y);
}
// Driver Code
const arr = [ 1, 1, 2, 2, 3, 3, 4, 4, 4, 4, 5 ];
const x = 4, y = 5;
console.log(moreFrequent(arr, x, y));
Output
4