Maximum Frequency Number

Easy
0/40
Average time to solve is 10m
profile
Contributed by
91 upvotes
Asked in companies
HSBCIBMInfo Edge India (Naukri.com)

Problem statement

Ninja is given an array of integers that contain numbers in random order. He needs to write a program to find and return the number which occurs the maximum times in the given input. He needs your help to solve this problem.

If two or more elements contend for the maximum frequency, return the element which occurs in the array first i.e. whose index is lowest.

For example,

For 'arr' = [ 1, 2, 3, 1, 2]. you need to return 1.
Detailed explanation ( Input/output format, Notes, Images )
Input Format:
The first line contains an integer 'T' which denotes the number of test cases or queries to be run.

The first line of each test case contains a single integer ‘N’ denoting the size of the array.

The second line of each test case contains ‘N’ space-separated integers denoting the elements of the array.
Output Format:
For each case, we need to print an integer that has the maximum frequency.

The output of each test case will be printed in a separate line.
Note:
You do not need to input or print anything, and it has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 5
1 <= N <= 10000
-10 ^ 3 <= |arr| <= 10 ^ 3

Time Limit: 1 sec
Sample Input 1:
1 
13
2 12 2 11 -12 2 -1 2 2 11 12 2 -6 
Sample Output 1:
2
Explanation of Sample Input 1:
Test case 1:
For the first test case of sample output 1, as we start traveling the array, ‘2’ has the highest frequency, hence our answer is ‘2’.
Sample Input 2:
2 
3
4 -5 1
4
1 -2 1 -2
Sample Output 2:
4
-2
Explanation of Sample Input 2:
Test case 1:
For the first test case of sample output 2, as all the elements have only occurred once, so we will take the first element that has occurred once. Therefore our output will be ‘4’.

Test case 2:
-2 has the highest frequency.
Hint

Hold one element at a time and find its frequency

Approaches (2)
Brute Force Approach

Here, we can simply run two loops. The outer loop picks all elements one by one and the inner loop finds the frequency of the picked element and compares it with the maximum present so far.

 

Algorithm:

 

  • Declare 4 variables as ‘maxFrequency’ , ‘currentFrequency’ , ‘maxElement’ , ‘currentElement’ and initialize them with 0
  • Run a loop from ‘i’ = ‘0’ to ‘N’
    • Set ‘currentElement’ as arr[i] and ‘currentFrequency’ to 0
    • Run a loop ‘j’ = ‘i’ to ‘N’
      • If ‘arr[j]’ is equal to currentElement
        • Increment currentFrequency by 1
    • If our ‘currentFrequency’ is greater than ‘maxFrequency’
      • Update ‘maxFrequency’ and ‘maxElement' with 'currentFrequency' and ‘currentFrequency’ respectively.
  • Return 'maxElement'

 

Time Complexity

O(N ^ 2) where N is the length of the array.

 

We iterate through the array to fixed our starting point and for every starting point, we make a loop that will count the frequency of the picked element. Therefore we are doing N + (N - 1) + (N - 2) + ..+ 1 = (N * ( N + 1)) / 2 iterations. So our Time Complexity is O(N ^ 2).

 

Space Complexity

O(1)

 

We use only some variables that require constant space.

Code Solution
(100% EXP penalty)
Maximum Frequency Number
Full screen
Console