- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Count all elements in the array which appears at least K times after their first occurrence in C++

In this tutorial, we will be discussing a program to find the number of elements in the array which appears at least K times after their first occurrence.

For this we will be provided with an integer array and a value k. Our task is to count all the elements occurring k times among the elements after the element in consideration.

## Example

#include <iostream> #include <map> using namespace std; //returning the count of elements int calc_count(int n, int arr[], int k){ int cnt, ans = 0; //avoiding duplicates map<int, bool> hash; for (int i = 0; i < n; i++) { cnt = 0; if (hash[arr[i]] == true) continue; hash[arr[i]] = true; for (int j = i + 1; j < n; j++) { if (arr[j] == arr[i]) cnt++; //if k elements are present if (cnt >= k) break; } if (cnt >= k) ans++; } return ans; } int main(){ int arr[] = { 1, 2, 1, 3 }; int n = sizeof(arr) / sizeof(arr[0]); int k = 1; cout << calc_count(n, arr, k); return 0; }

## Output

1

- Related Questions & Answers
- Program to find elements from list which have occurred at least k times in Python
- Find all elements in array which have at-least two greater elements in C++
- Count pairs (p, q) such that p occurs in array at least q times and q occurs at least p times in C++
- Maximum value K such that array has at-least K elements that are >= K in C++
- Program to find k where k elements have value at least k in Python
- First element that appears even number of times in an array in C++
- Maximum sum subsequence with at-least k distant elements in C++
- Count elements that are divisible by at-least one element in another array in C++
- Count all pairs of an array which differ in K bits in C++
- Maximum sum subsequence with at-least k distant elements in C++ program
- Count the number of elements in an array which are divisible by k in C++
- Count how many times the substring appears in the larger String in Java
- Count substrings with each character occurring at most k times in C++
- Unique element in an array where all elements occur k times except one in C++
- Count number of times value appears in particular column in MySQL?

Advertisements