The most frequent number that is the number that occurs the highest number of times

Let's say I have the next array of 9 numbers: 1, 2, 2, 4, 2, 2, 3, 2, 1.
I want to say that 2 has the highest frequency in the array. But I thought I could write this in one word. I also thought of "2 is the most frequent". Which is the best way to say that a number is the most frequent in an array?

The most frequent number that is the number that occurs the highest number of times

Em.

45.1k12 gold badges134 silver badges147 bronze badges

asked May 22, 2017 at 7:48

The most frequent number that is the number that occurs the highest number of times

1

Two is the most frequent (spelled frequent) sounds fine to me. Alternatively, you could write something like

The number two is the most frequent value in the given array.

I think the "one word" you are looking for is mode. It is mathematical terminology.

In the given array, two is the mode.

  • mode
    The mode is the value that appears most often in a set of data.
    (Wikipedia)

answered May 22, 2017 at 7:55

The most frequent number that is the number that occurs the highest number of times

Em.Em.

45.1k12 gold badges134 silver badges147 bronze badges

1

"Most frequent" seems reasonable to me. If you really need a single word for some reason, you could choose commonest:

commonest

superlative form of common: most common.

common

Occurring or happening regularly or frequently; usual.


For a mathematically-rigorous word, you should use mode:

mode

(statistics) The most frequently occurring value in a distribution.

answered May 22, 2017 at 10:50

The most frequent number that is the number that occurs the highest number of times

Toby SpeightToby Speight

2,01012 silver badges23 bronze badges

6

Some more suitable words: Predominant, Preponderant, Principal. All can be used to mean "most frequent", and are better known than "mode" (which is rarely if ever used by anyone who isn't a mathematician) while also sounding more professional than something like "commonest".

E.g. "The preponderant number is 2".

answered May 22, 2017 at 15:03

The most frequent number that is the number that occurs the highest number of times

BenubirdBenubird

2491 silver badge4 bronze badges

4

You could say "prevalent" or "abound"

For arrays the fixed phrase "most frequent value" is widely used. In mathematics the term "mode" is common. Here's a good explanation of what a mode is - Most frequent values in array.

answered May 22, 2017 at 15:40

The most frequent number that is the number that occurs the highest number of times

SovereignSunSovereignSun

24.6k34 gold badges136 silver badges253 bronze badges

As the question mentions an array of numbers, seems to me mode is absolutely the correct answer (and had I got here quicker I think that would have been my one-word answer).

This used to be taught at primary level in the UK (until 2015), so not obscure terminology :)

answered May 22, 2017 at 22:30

The most frequent number that is the number that occurs the highest number of times

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .

The most frequent number that is the number that occurs the highest number of times

Level: Medium
Asked in: Facebook, Uber

Understanding the Problem

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:-

  • What should I return if two or more elements have the maximum frequency? (Ans: Return whichever element is the smallest)
  • Can negative numbers exist in the array? (Ans: Yes, they can)

Brute force and Efficient Solutions

We will discuss three possible solutions for this problem:-

  1. Brute Force: Calculate frequency using nested loops
  2. Using Sorting: Calculate frequency linearly after sorting
  3. Using Hash Table : Use a Hash Table to find the frequency of elements

1. Brute Force Approach

For each element, scan the entire array to find its duplicates. Maintain two variables max_freq and ans tostore 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!
  • According to this algorithm, the frequency is calculated again upon encountering the second occurrence of an element. How should we optimize this such that frequency is calculated only once for each unique element?
  • How can we improve the time complexity further?

2. Using Sorting

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!
  • If the frequency of the current element is equal to the maximum element, is it necessary to check if the current element is smaller than ans?
  • How could you use the concept of time-space tradeoff to your benefit and find the frequency of all elements faster?

3. Using Hash Table

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

  • If exist, increment the value of that key by 1
  • If not, store the value as 1

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!
  • Is using a sophisticated data structure like BST useful here? Analyze the complexity in that case.
  • What if all elements were unique, i.e, the maximum frequency is 1, will this code return output as expected? What changes will you make?

Comparison of different solutions

The most frequent number that is the number that occurs the highest number of times

Suggested problems to solve

  • Find the most frequent word in a sentence
  • Sort characters by frequency
  • Find the frequency of all words in a sentence
  • Find the least frequent element in the array
  • Find the top k frequent elements in the array
  • Find the smallest subarray with all occurrences of the most frequent element

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 Data Structure And Algorithms Online Course - Admissions Open

What is the most frequently occurring value called?

The mode is the most commonly occurring value in a distribution. This table shows a simple frequency distribution of the retirement age data. The most commonly occurring value is 54, therefore the mode of this distribution is 54 years.

What is median in statistics?

Median, in statistics, is the middle value of the given list of data when arranged in an order. The arrangement of data or observations can be made either in ascending order or descending order. Example: The median of 2,3,4 is 3. In Maths, the median is also a type of average, which is used to find the centre value.

What do you call the value that occurs with the highest frequency in a data set?

In statistics, the mode represents the most common value in a data set. Therefore, the mode is the value that occurs with the highest frequency in a data set (Mann 2012).

Which is the most frequent value mean median mode?

The mode is found by collecting and organizing the data in order to count the frequency of each result. Thus mode is the most frequently observed value.