Given an array of size n and a number k, fin all elements that appear more than ” n/k ” times.

Elements that appear more than n/k times in Python

User will provide us value of n, k, array. Where n is basically the length of array. We are required to find all the elements whose occurrence in array1 is more than or equal to n/k times. We will take floor value of n/k times.
Example :

  • k = 3, n = 7
  • array = [4,5,6,7,8,4,4]
  • floor(n/k) = floor(7/3) = 2
  • Output: 4
elements that appear more than n by k times in python

Algorithm

  • Step 1: Create necessary elements and provide value (array1, n, k)
  • Step 2: Call the function find

Algorithm for function find

  • Step 1: Take a variable a where you will store floor of n/k
  • Step 2: Make a list name final where we will store all the elements whose occurrence is more than or equal to a
  • Step 3: Iterate on the elements of array and check

        ->  If the elements count is more than or equal to a and if the element is not in final then append that element to final list.

  • Step 4: Print elements of final

Python Code

Run
import math


def find(k, n, array):
    a = n / k
    a = math.floor(a)
    final = []
    for i in array:
        if array.count(i) >= a and i not in final:
            final.append(i)
    print("Elements whose occurrence is more than or equal to", a, "times =", final)


k = 3
n = 7
array = [4, 5, 6, 7, 8, 4, 4]

find(k, n, array)

Output

Elements whose occurrence is more than or equal to 2 times = [4]

Comments