AfterAcademy Tech
•
05 Nov 2019

Level: Medium
Asked in: Facebook, Uber
Problem Description: Given an array A[] of size n, find the most frequent element in the array, i.e. the element which occurs the most number of times. It is assured that at least one element is repeated.
For example:
Input: A[] = {3, 9, 1, 3, 6, 3, 8, 1, 6}
Output: 3
Input: A[] = {1, 9, 1, 3, 2, 3, 10}
Output: 1
Possible questions to ask the interviewer:-
We will discuss three possible solutions for this problem:-
For each element, scan the entire array to find its duplicates. Maintain two variables max_freq and ans to store the maximum frequency and the element with that frequency respectively.
Pseudo-Code
int findMostFrequentElement(int A[], int n)
{
int max_freq = 0
int ans = -1
for (i = 0 to n-1)
{
int curr_freq = 1
for (j = i+1 to n-1)
if (A[j] == A[i])
curr_freq = curr_freq + 1
if (max_freq < curr_freq)
{
max_freq = curr_freq
ans = A[i]
}
else if (max_freq == curr_freq)
ans = min(ans, A[i])
}
return ans
}
Complexity Analysis
Time Complexity: O(n²) (Why?)
Space Complexity: O(1)
Critical ideas to think!
If we sort the array, all the duplicate elements will get lined up next to each other. We can now linearly find the frequency of all elements in the array. This approach also ensures that frequency is calculated only once for each unique element.
Pseudo-Code
int findMostFrequentElement(int A[], int n)
{
sort(A, n)
int max_freq = 0
int ans = -1
int i = 0
while (i < n)
{
int curr_freq = 1
while (i+1 < n && A[i+1] == A[i])
{
curr_freq = curr_freq + 1
i = i + 1
}
if (max_freq < curr_freq)
{
max_freq = curr_freq
ans = A[i]
}
else if (max_freq == curr_freq)
ans = min(ans, A[i])
i = i + 1
}
return ans
}
Complexity Analysis
Time Complexity: Sorting the array + Linear Traversal of array
= O(nlogn) + O(n) = O(n)
Space Complexity: O(n), if we use merge sort and O(1) if we use heap sort
Critical ideas to think!
We can create a hash table and store elements and their frequency counts as key value pairs.
Solution Steps
1. Create a Hash Table to store frequency of each element in the given array. Consider elements in the array as key and their frequency as value
2. First scan the array one by one and check if value associated with any key (as that particular element) exist in the Hash Table or not
3. During the iteration, we are also storing the most frequent element and its frequency in the parameter ans and max_freq respectively.
4. Update ans and max_freq if any value (frequency for that element) greater than the stored frequency is found.
5. And finally, return the most frequent element found i.e. return ans
Pseudo-Code
int findMostFrequentElement(int A[], int n)
{
Create a HashTable H
int max_freq = 1
int ans = -1
for (i = 1 to n-1)
{
if (A[i] in H)
{
H[A[i]] = H[A[i]] + 1
if (max_freq < H[A[i]])
{
max_freq = H[A[i]]
ans = A[i]
}
else if (max_freq == H[A[i]])
ans = min(ans, A[i])
}
else
H[A[i]] = 1
}
return ans
}
Complexity Analysis
Time Complexity: O(n) (Why?)
Space Complexity: O(n), for storing the Hash Table
Critical ideas to think!

If you have any more approaches or you find an error/bug in the above solutions, please comment down below.
Happy Coding! Enjoy Algorithms!
AfterAcademy Tech
Given an array A[] of size n, you need to find the next greater element for each element in the array. Return an array that consists of the next greater element of A[i] at index i.

AfterAcademy Tech
You are given an array A[] consisting of n elements. You need to find and return the number which appears more than n/2 times.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)

AfterAcademy Tech
Given an array A[] of size n, you need to find the maximum and minimum element present in the array.Your algorithm should make minimum number of comparisons.
