# Find the only element that appears b times

Given an array where every element occurs a times, except one element which occurs b (a>b) times. Find the element that occurs b times.

**Examples:**

Input : arr[] = [1, 1, 2, 2, 2, 3, 3, 3] a = 3, b = 2 Output : 1

Add each number once and multiply the sum by a, we will get a times the sum of each element of the array. Store it as a_sum. Subtract the sum of the whole array from the a_sum and divide the result by (a-b). The number we get is the required number (which appears b times in the array).

## C++

`// CPP program to find the only element that ` `// appears b times ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `appearsbTimes(` `int` `arr[], ` `int` `n, ` `int` `a, ` `int` `b) ` `{ ` ` ` `unordered_set<` `int` `> s; ` ` ` ` ` `int` `a_sum = 0, sum = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) { ` ` ` `if` `(s.find(arr[i]) == s.end()) { ` ` ` `s.insert(arr[i]); ` ` ` `a_sum += arr[i]; ` ` ` `} ` ` ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` ` ` `a_sum = a * a_sum; ` ` ` ` ` `return` `((a_sum - sum) / (a - b)); ` `} ` ` ` `int` `main() ` `{ ` ` ` `int` `arr[] = { 1, 1, 2, 2, 2, 3, 3, 3 }; ` ` ` `int` `a = 3, b = 2; ` ` ` `int` `n = ` `sizeof` `(arr) / ` `sizeof` `(arr[0]); ` ` ` `cout << appearsbTimes(arr, n, a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find the only element that ` `// appears b times ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` `static` `int` `appearsbTimes(` `int` `arr[], ` `int` `n, ` ` ` `int` `a, ` `int` `b) ` `{ ` ` ` `HashSet<Integer> s = ` `new` `HashSet<Integer>(); ` ` ` ` ` `int` `a_sum = ` `0` `, sum = ` `0` `; ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < n; i++) ` ` ` `{ ` ` ` `if` `(!s.contains(arr[i])) ` ` ` `{ ` ` ` `s.add(arr[i]); ` ` ` `a_sum += arr[i]; ` ` ` `} ` ` ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` `a_sum = a * a_sum; ` ` ` ` ` `return` `((a_sum - sum) / (a - b)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `arr[] = { ` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `3` `, ` `3` `, ` `3` `}; ` ` ` `int` `a = ` `3` `, b = ` `2` `; ` ` ` `int` `n = arr.length; ` ` ` `System.out.println(appearsbTimes(arr, n, a, b)); ` `} ` `} ` ` ` `// This code is contributed by 29AjayKumar ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to find the only ` `# element that appears b times ` `def` `appearsbTimes(arr, n, a, b): ` ` ` ` ` `s ` `=` `dict` `() ` ` ` ` ` `a_Sum ` `=` `0` ` ` `Sum` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(n): ` ` ` `if` `(arr[i] ` `not` `in` `s.keys()): ` ` ` `s[arr[i]] ` `=` `1` ` ` `a_Sum ` `+` `=` `arr[i] ` ` ` ` ` `Sum` `+` `=` `arr[i] ` ` ` ` ` `a_Sum ` `=` `a ` `*` `a_Sum ` ` ` ` ` `return` `((a_Sum ` `-` `Sum` `) ` `/` `/` `(a ` `-` `b)) ` ` ` `# Driver code ` `arr ` `=` `[` `1` `, ` `1` `, ` `2` `, ` `2` `, ` `2` `, ` `3` `, ` `3` `, ` `3` `] ` `a, b ` `=` `3` `, ` `2` `n ` `=` `len` `(arr) ` `print` `(appearsbTimes(arr, n, a, b)) ` ` ` `# This code is contributed by mohit kumar ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find the only element that ` `// appears b times ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` `static` `int` `appearsbTimes(` `int` `[]arr, ` `int` `n, ` ` ` `int` `a, ` `int` `b) ` `{ ` ` ` `HashSet<` `int` `> s = ` `new` `HashSet<` `int` `>(); ` ` ` ` ` `int` `a_sum = 0, sum = 0; ` ` ` ` ` `for` `(` `int` `i = 0; i < n; i++) ` ` ` `{ ` ` ` `if` `(!s.Contains(arr[i])) ` ` ` `{ ` ` ` `s.Add(arr[i]); ` ` ` `a_sum += arr[i]; ` ` ` `} ` ` ` ` ` `sum += arr[i]; ` ` ` `} ` ` ` `a_sum = a * a_sum; ` ` ` ` ` `return` `((a_sum - sum) / (a - b)); ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[]arr = { 1, 1, 2, 2, 2, 3, 3, 3 }; ` ` ` `int` `a = 3, b = 2; ` ` ` `int` `n = arr.Length; ` ` ` `Console.WriteLine(appearsbTimes(arr, n, a, b)); ` `} ` `} ` ` ` `// This code is contributed by Princi Singh ` |

*chevron_right*

*filter_none*

**Output:**

1

Please refer below article for more approaches.

Find the only element that appears k times.

## Recommended Posts:

- First element that appears even number of times in an array
- Find the element that appears once in an array where every other element appears twice
- Find the element that appears once
- Find the element that appears once in a sorted array
- Remove characters from string that appears strictly less than K times
- Check if max occurring character of one string appears same no. of times in other
- Count all elements in the array which appears at least K times after their first occurrence
- Maximum number of times str1 appears as a non-overlapping substring in str2
- Find the K-th minimum element from an array concatenated M times
- Maximum difference between two elements such that larger element appears after the smaller number
- First element occurring k times in an array
- Largest element in the array that is repeated exactly k times
- Smallest element in an array that is repeated exactly 'k' times.
- Sum of array elements after every element x is XORed itself x times
- Element which occurs consecutively in a given subarray more than or equal to K times

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.