Asked inWalmart,SDE-2
Maximum Frequency Number

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.
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
CodingNinjas
author
2y
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 presen...read more

CodingNinjas
author
2y
Greedy Approach

We can use hashing and count the frequency of all the elements and simultaneously find the maximum frequency. Now we can iterate through the array and find the first element with the ma...read more

Kaushaki Kumari
1y
currently not working

int maxcount = 0;

int element_having_max_freq = 0;

for (int i = 0; i < n; i++) {

int count = 0;

for (int j = 0; j < n; j++) {

if (arr[i] == arr[j]) {

count++;

}

}

if (count > maxcount) {

maxcount =...read more

Kaushaki Kumari
1y
currently not working

int maxcount = 0;

int element_having_max_freq = 0;

for (int i = 0; i < n; i++) {

int count = 0;

for (int j = 0; j < n; j++) {

if (arr[i] == arr[j]) {

count++;

}

}

if (count > maxcount) {

maxcount =...read more

Kaushaki Kumari
1y
currently not working

int maxcount = 0;

int element_having_max_freq = 0;

for (int i = 0; i < n; i++) {

int count = 0;

for (int j = 0; j < n; j++) {

if (arr[i] == arr[j]) {

count++;

}

}

if (count > maxcount) {

maxcount =...read more

Kimmi Mams
1y
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...read more
Add answer anonymously...
Walmart SDE-2 Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions
Get AmbitionBox app

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2024 Info Edge (India) Ltd.

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter