Javatpoint Logo
Javatpoint Logo

Find an Index of Maximum Occurring Element with Equal Probability using Python

Introduction:

In this tutorial, we learn how to find an index of maximum occurring element with equal probability using Python. If we take an input as {1, 2, 5, 3, 4, 5, 6, 5, 7, 8, 5, 9} here, 5 is taken in four times. The index number 5 is 2, 5, 7 and 10. Here, we are given a solution that returns an index of the maximum occurring element with equal probability. If the array has two largest occurrences, the solution should consider the first largest occurrence.

Example:

Here, give an example of array index input and its corresponding output.

Algorithm:

Here, we learn the algorithm to find an index of maximum occurring element with equal probability using Python.

  1. Firstly, define the function findIndexOfMaxElement with two parameters, which are arr and n
  2. Then, keep the count of all input elements on the map.
  3. Now, we create a for loop.
  4. Then, traversing this map.
  5. After that, we need to find which element first occurs in maximum time.
  6. Then, generate an "n" random number between 1 and count the element occurring at maximum time in the map.
  7. Now we initialize the array input arr[]
  8. Lastly, traversing the input. Then, it returns the nth occurrence of the maximum occurring element's index value.

Program code:

Here, we give the program code in Python to find an index of maximum occurring element with equal probability. The code is now given below -

Output:

Now, we compile the above code in Python, and after successful compilation, we run it. The output is given below -

5 is the element with maximum frequency, which is present at index:  11

Conclusion:

So, in this tutorial, we are learning how to find an index of maximum occurring elements with equal probability using Python. Mainly here, we check which element occurs the maximum time in the given map and then find the maximum occurrence element index value randomly. We also learn the algorithm of this program. Depending upon this algorithm, we write the above Python code and share its output.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA