- 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

# Find the only element that appears b times using C++

In this problem, we are given an arr[] of size n and two integers a and b. Our task is to *f**ind the only element that appears b times*.

All values of the array occur a time except one value which occurs b times in the array and we need to find that value.

**Let’s take an example to understand the problem,**

**Input**

arr[] = {3, 3, 3, 3, 5, 5, 5, 1, 1,1,1} a = 4, b = 3

**Output**

5

## Solution Approach

A simple solution to the problem is by counting the occurrence of each element and then storing it in a 2D matrix. Then traversing the matrix to find the value with occurrence frequency b.

The time complexity of this approach is O(N^{2}) but a more effective approach that can solve the problem is by finding the sum of all unique elements of the array and then multiplying it by a. Then subtract the sum of the whole array from this value and then divide the result by (a-b). The resulting value is the value whose occurrence frequency is b.

## Example

Program to illustrate the working of our solution

#include <bits/stdc++.h> using namespace std; int findbFreqVal(int arr[], int n, int a, int b){ unordered_set<int> uniqueVal; int uniqueValSum = 0, arrSum = 0; for (int i = 0; i < n; i++) { if (uniqueVal.find(arr[i]) == uniqueVal.end()) { uniqueVal.insert(arr[i]); uniqueValSum += arr[i]; } arrSum += arr[i]; } uniqueValSum = a * uniqueValSum; return ((uniqueValSum - arrSum) / (a - b)); } int main(){ int arr[] = { 4, 4, 4, 31, 8, 8, 8, 5, 5, 5}; int a = 3, b = 1; int n = sizeof(arr) / sizeof(arr[0]); cout<<"The value of the array that appears b times is "<<findbFreqVal(arr, n, a, b); return 0; }

## Output

The value of the array that appears b times is 31

- Related Questions & Answers
- 8085 program to find the element that appears once
- Take an array and find the one element that appears an odd number of times in JavaScript
- Find the element that appears once in sorted array - JavaScript
- Find the element that appears once in an array where every other element appears twice in C++
- Return the element that appears for second most number of times in the array JavaScript
- Finding number that appears for odd times - JavaScript
- First element that appears even number of times in an array in C++
- Get the item that appears the most times in an array JavaScript
- How to find the one integer that appears an odd number of times in a JavaScript array?
- Find the only different element in an array using C++
- Find three element from different three arrays such that that a + b + c = sum in Python
- Finding two missing numbers that appears only once and twice respectively in JavaScript
- Number of times a string appears in another JavaScript
- Program to find how many times a character appears in a string in PHP
- Count how many times the substring appears in the larger String in Java