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

Get Prepinsta Prime

Get all 200+ courses offered by Prepinsta

Never Miss an OffCampus Update

Get OffCampus Updates on Social Media from PrepInsta

Follow us on our Media Handles, we post out OffCampus drives on our Instagram, Telegram, Discord, Whatsdapp etc.

Get Hiring Updates
Amazon,Google,Delottie & 30+companies are hiring ! Get hiring Updates right in your inbox from PrepInsta

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others.

Get PrepInsta Prime Subscription

Get access to all the courses that PrepInsta offers, check the out below -

Companies

TCS, Cognizant, Delloite, Infosys, Wipro, CoCubes, KPMG, Amazone, ZS Associates, Accenture, Congnizant & other 50+ companies

Programming

Data Structures, Top 500 Codes, C, C++, Java Python & other 10+ subjects

Skills

Full Stack Web Development, Data Science, Machine Learning, AWS Cloud, & other 10+ skills and 20+ projects

OffCampus Updates

Never Miss OffCampus Updates

Get OffCampus Update from PrepInsta

Follow us on our Social Media Handles

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