# Largest number in the Array having frequency same as value

Given an array **arr** containing **N** integers, the task is to find the largest number in the array whose frequency is equal to its value. If no such number exists, then print -1.

**Examples:**

Input:arr = [3, 2, 5, 2, 4, 5]

Output:2

Explanation:

In this given array frequency of 2 is 2, whereas the frequency of the remaining numbers doesnt match with itself. So the answer is 2.

Input:arr = [3, 3, 3, 4, 4, 4, 4]

Output:4

Explanation:

In this given array frequency of 3 is 3 and 4 is 4 but largest number is 4. So the answer is 4.

Input:arr = [1, 1, 1, 2, 3, 3]

Output:-1

Explanation:

There is no such number in the given array whose frequency is equal to itself. Thus the output is -1.

**Simple Approach:**

- Create a new array to keep the count of the occurrences in the given array.
- Traverse the new array in reverse order.
- Return the first number whose count is equal to itself.

Below is the implementation of the above approach:

## C++

`// C++ solution to the above problem ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the largest number ` `// whose frequency is equal to itself. ` `int` `findLargestNumber(vector<` `int` `>& arr) ` `{ ` ` ` ` ` `// Find the maximum element in the array ` ` ` `int` `k = *max_element(arr.begin(), ` ` ` `arr.end()); ` ` ` `int` `m[k] = {}; ` ` ` ` ` `for` `(` `auto` `n : arr) ` ` ` `++m[n]; ` ` ` ` ` `for` `(` `auto` `n = arr.size(); n > 0; --n) { ` ` ` `if` `(n == m[n]) ` ` ` `return` `n; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr = { 3, 2, 5, 2, 4, 5 }; ` ` ` ` ` `cout << findLargestNumber(arr) << endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java solution to the above problem ` `import` `java.util.*; ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// whose frequency is equal to itself. ` `static` `int` `findLargestNumber(` `int` `[] arr) ` `{ ` ` ` ` ` `// Find the maximum element in the array ` ` ` `int` `k = Arrays.stream(arr).max().getAsInt(); ` ` ` `int` `[]m = ` `new` `int` `[k + ` `1` `]; ` ` ` ` ` `for` `(` `int` `n : arr) ` ` ` `++m[n]; ` ` ` ` ` `for` `(` `int` `n = arr.length - ` `1` `; n > ` `0` `; --n) ` ` ` `{ ` ` ` `if` `(n == m[n]) ` ` ` `return` `n; ` ` ` `} ` ` ` `return` `-` `1` `; ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `int` `[] arr = { ` `3` `, ` `2` `, ` `5` `, ` `2` `, ` `4` `, ` `5` `}; ` ` ` ` ` `System.out.print(findLargestNumber(arr) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

## C#

`// C# solution to the above problem ` `using` `System; ` `using` `System.Linq; ` ` ` `class` `GFG{ ` ` ` `// Function to find the largest number ` `// whose frequency is equal to itself. ` `static` `int` `findLargestNumber(` `int` `[] arr) ` `{ ` ` ` ` ` `// Find the maximum element in the array ` ` ` `int` `k = arr.Max(); ` ` ` `int` `[]m = ` `new` `int` `[k + 1]; ` ` ` ` ` `foreach` `(` `int` `n ` `in` `arr) ` ` ` `++m[n]; ` ` ` ` ` `for` `(` `int` `n = arr.Length - 1; n > 0; --n) ` ` ` `{ ` ` ` `if` `(n == m[n]) ` ` ` `return` `n; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `public` `static` `void` `Main(String[] args) ` `{ ` ` ` `int` `[] arr = { 3, 2, 5, 2, 4, 5 }; ` ` ` ` ` `Console.Write(findLargestNumber(arr) + ` `"\n"` `); ` `} ` `} ` ` ` `// This code is contributed by amal kumar choubey ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)

**Auxiliary Space Complexity:** O(N)

**Another Approach:**

**Note:** This approach is valid only when the numbers in the given array are less than 65536 i.e. 2^{16}.

- Here, use the input array to store the count.
- Since the values are limited, simply use the first half(first 16 bits) of the integer for keeping the count by adding 65536.
- Use the right shift operator(right shift by 16 bits) while traversing the array in reverse order and return the first number whose count is equal to itself.

Below is the implementation of the above approach:

`// C++ code for the above problem ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to find the largest number ` `// whose frequency is equal to itself. ` `int` `findLargestNumber(vector<` `int` `>& arr) ` `{ ` ` ` `for` `(` `auto` `n : arr) { ` ` ` `n &= 0xFFFF; ` ` ` `if` `(n <= arr.size()) { ` ` ` `// Adding 65536 to keep the ` ` ` `// count of the current number ` ` ` `arr[n - 1] += 0x10000; ` ` ` `} ` ` ` `} ` ` ` ` ` `for` `(` `auto` `i = arr.size(); i > 0; --i) { ` ` ` `// right shifting by 16 bits ` ` ` `// to find the count of the ` ` ` `// number i ` ` ` `if` `((arr[i - 1] >> 16) == i) ` ` ` `return` `i; ` ` ` `} ` ` ` `return` `-1; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `vector<` `int` `> arr ` ` ` `= { 3, 2, 5, 5, 2, 4, 5 }; ` ` ` ` ` `cout << findLargestNumber(arr) ` ` ` `<< endl; ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

2

**Time Complexity:** O(N)

**Auxiliary Space Complexity:** O(1)

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: **DSA Self Paced**. Become industry ready at a student-friendly price.

## Recommended Posts:

- Find the frequency of a number in an array
- Count number of occurrences (or frequency) in a sorted array
- Maximum length prefix such that frequency of each character is atmost number of characters with minimum frequency
- Number of ways to change the Array such that largest element is LCM of array
- Largest number dividing maximum number of elements in the array
- Largest subarray with frequency of all elements same
- Python | Element with largest frequency in list
- Largest index for each distinct character in given string with frequency K
- Largest palindromic number in an array
- Largest perfect cube number in an Array
- Largest number in an array that is not a perfect cube
- Largest Divisor for each element in an array other than 1 and the number itself
- Generate a number such that the frequency of each digit is digit times the frequency in given number
- Assembly language program to find largest number in an array
- Largest number in given Array formed by repeatedly combining two same elements
- Length of the largest substring which have character with frequency greater than or equal to half of the substring
- Replace every elements in the array by its frequency in the array
- Sum of all odd frequency elements in an array
- Sorting Array Elements By Frequency | Set 3 (Using STL)
- Sort an array of Strings according frequency

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.