How to Find the Frequency of an Element in a Set in C++? (original) (raw)
Last Updated : 28 Feb, 2024
C++ STL provides a set container that can be used to store unique elements in a sorted order. In this article, we will learn how to find the frequency of an element in a set in C++.
**Example:
**Input:
sets ={10,20,30,40,50}
**Output:
Frequency of 30 is 1
Finding Frequency of an Element in a Set in C++
In C++, the set stores the unique element, so if the element is present in the set, its frequency cannot be more than 1. But std::set still has the std:📐:count() function that can be used to find the frequency of a specific element. This function returns 0 if the element is not present and 1 if the element is present.
C++ Program to Find the Frequency of an Element in a Set
The below example demonstrates how we can find the frequency of a specific element in a set.
C++ `
// C++ program to illustrate how to find the frequency of a // specific element in a set #include #include #include using namespace std;
int main() {
// Intializing a set with few elements
set<int> mySet = { 10, 20, 30, 20, 40, 20, 50 };
// Print the elements of the set
cout << "Set Elements: ";
for (auto it : mySet) {
cout << it << " ";
}
cout << endl;
// Choose target element
int targetElement = 30;
// Use the count function to find the frequency of the
// target element
int frequency = mySet.count(targetElement);
// Output the frequency of the target element
cout << "Frequency of " << targetElement
<< " is: " << frequency << endl;
return 0;
}
`
Output
Set Elements: 10 20 30 40 50 Frequency of 30 is: 1
**Time Complexity: O(N), where N is the total number of elements in the set.
**Auxiliary Space: O(1)
Similar Reads
- How to Find Frequency of an Element in a Vector in C++? In C++, vectors are containers that store the elements in contiguous memory locations just like arrays. The frequency of a specific element means how many times that particular element occurs in a vector. In this article, we will learn how to find the frequency of a specific element in a vector in C 2 min read
- How to Find the Frequency of an Element in a Multiset in C++? In C++, a multiset is a container that stores elements in a sorted order and multiple elements can have the same values. In this article, we will learn how to find the frequency of a specific element in a multiset. Example: Input: myMultiset = { 5,2,8,5,8,8} Element: 8 Output: Frequency of 8 is: 3Fi 2 min read
- How to Find Frequency of an Element in a List in C++? In C++, lists are sequence containers that allow non-contiguous memory allocation. They are implemented as doubly-linked lists. The frequency of a specific element means how many times that particular element occurs in a list. In this article, we will learn how to find the frequency of a specific el 2 min read
- How to Find the First Element in a Set in C++? In C++, a set is a container that stores unique elements following a specific order. In this article, we will learn how to find the first element in a set. Example: Input: mySet = {1, 2, 4, 3, 8, 4, 7, 8, 6, 4} Output: First Element = 1Find the First Element in a Set in C++To find the first element 1 min read
- How to Find the Frequency of Vector Elements in a Multiset in C++? In C++, the frequency of vector elements in a multiset means how many times the particular element of a vector occurs in a multiset. In this article, we will learn how to find the frequency of vector elements in a multiset in C++. For Example, Input: vectorvec = {5, 1, 3, 2, 4};multiset 2 min read
- How to Find the Last Element in a Set in C++? In this article, we will learn how to find the last element is a set in C++.The most efficient way to find the last element of set is by using set rbegin() function. Let’s take a look at a simple example:C++#include <bits/stdc++.h> using namespace std; int main() { set s = {1, 3, 4, 2 min read
- C++ Program to Find the Frequency of Elements in an Array In C++, arrays are a type of data structure that can store a fixed-size sequential collection of elements of the same type. In this article, we will learn how to find the frequency of elements in an array in C++. Example: Input: Array: {1, 2, 3, 4, 2, 1, 3, 2, 4, 5} Output: Element: 1, Frequency: 2 2 min read
- How to Find the Last Occurrence of an Element in a Set in C++? In C++, a set is a container that stores unique elements in a sorted order and elements are accessed and traversed using iterators. In this article, we will learn how to find the last occurrence of a specific element in a set in C++. Example Input:set s = {1, 2, 3, 4, 5, 6, 7, 8, 9}; Key 2 min read
- How to Find First Occurrence of an Element in a Set in C++? In C++, a set is an ordered container that stores its unique values in some given order. In this article, we will see how to find the first occurrence of a specific element in a set in C++ STL. For Example, Input: mySet = {1, 2, 3, 8, 9, 11} Target = 9 Output: Element found at Index: 4Find the First 2 min read
- Find index of an element in a Set in C++ Given a set S consisting of N integers and an element K, the task is to find the index of the element K in the set S. If the element is not present in S, print -1. Examples: Input: N = 5, S = {1, 2, 3, 4, 6} K = 6Output: 5Explanation: 6 is the 5th element in S. Input: N = 5, S = {1, 2, 3, 4, 6}, K = 2 min read