
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find elements from list which have occurred at least k times in Python
Suppose we have a list of elements called nums, and a value k. We have to find those elements which have occurred at least k number of times.
So, if the input is like nums = [2,5,6,2,6,1,3,6,3,8,2,5,9,3,5,1] k = 3, then the output will be [2, 5, 6, 3]
To solve this, we will follow these steps −
- c := a list containing frequencies of each elements present in nums
- res := a new list
- for each key n in c, do
- if c[n] >= k, then
- insert n at the end of res
- if c[n] >= k, then
- return res
Example
Let us see the following implementation to get better understanding −
from collections import Counter def solve(nums, k): c = Counter(nums) res = [] for n in c: if c[n] >= k: res.append(n) return res nums = [2,5,6,2,6,1,3,6,3,8,2,5,9,3,5,1] k = 3 print(solve(nums, k))
Input
[2,5,6,2,6,1,3,6,3,8,2,5,9,3,5,1], 3
Output
[2, 5, 6, 3]
- Related Articles
- Program to find k where k elements have value at least k in Python
- Python program to find tuples which have all elements divisible by K from a list of tuples
- Find all elements in array which have at-least two greater elements in C++
- Program to find number of elements in A are strictly less than at least k elements in B in Python
- Program to remove string characters which have occurred before in Python
- Count all elements in the array which appears at least K times after their first occurrence in C++
- Program to find a sub-list of size at least 2 whose sum is multiple of k in Python
- Program to find largest average of sublist whose size at least k in Python
- Program to find three unique elements from list whose sum is closest to k Python
- Maximum sum subsequence with at-least k distant elements in C++ program
- Program to find maximum sum of popped k elements from a list of stacks in Python
- Program to find minimum sum subsequence by taking at least one element from consecutive 3 elements in python
- Program to find length of longest substring with character count of at least k in Python
- Program to find length of longest increasing subsequence with at least k odd values in Python
- Find top K frequent elements from a list of tuples in Python

Advertisements