📜  数组中最大的数字,其频率与值相同

📅  最后修改于: 2021-04-29 09:27:21             🧑  作者: Mango

给定一个包含N个整数的数组arr ,任务是在数组中找到频率等于其值的最大数字。如果不存在这样的数字,则打印-1。
例子:

简单方法:

  1. 创建一个新数组,以将给定数组中的出现次数保留下来。
  2. 以相反的顺序遍历新数组。
  3. 返回计数等于其自身的第一个数字。

下面是上述方法的实现:

C++
// C++ solution to the above problem
 
#include 
using namespace std;
 
// Function to find the largest number
// whose frequency is equal to itself.
int findLargestNumber(vector& 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 arr = { 3, 2, 5, 2, 4, 5 };
 
    cout << findLargestNumber(arr) << endl;
    return 0;
}


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


Python3
# Python3 solution to the above problem
 
# Function to find the largest number
# whose frequency is equal to itself.
def findLargestNumber(arr):
     
    # Find the maximum element in the array
    k = max(arr);
    m = [0] * (k + 1);
 
    for n in arr:
        m[n] += 1;
 
    for n in range(len(arr) - 1, 0, -1):
        if (n == m[n]):
            return n;
 
    return -1;
 
# Driver code
if __name__ == '__main__':
     
    arr = [ 3, 2, 5, 2, 4, 5 ];
 
    print(findLargestNumber(arr));
 
# This code is contributed by amal kumar choubey


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


C++
// C++ code for the above problem
 
#include 
using namespace std;
 
// Function to find the largest number
// whose frequency is equal to itself.
int findLargestNumber(vector& 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 arr
        = { 3, 2, 5, 5, 2, 4, 5 };
 
    cout << findLargestNumber(arr)
         << endl;
    return 0;
}


Java
// Java code for the above problem
class GFG{
 
// Function to find the largest number
// whose frequency is equal to itself.
static int findLargestNumber(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
    {
        arr[i] &= 0xFFFF;
        if (arr[i] <= n)
        {
             
            // Adding 65536 to keep the
            // count of the current number
            arr[i] += 0x10000;
        }
    }
 
    for(int i = n - 1; i > 0; --i)
    {
         
        // Right shifting by 16 bits
        // to find the count of the
        // number i
        if ((arr[i] >> 16) == i)
            return i + 1;
    }
    return -1;
}
 
// Driver code
public static void main(String[] args)
{
    int []arr = { 3, 2, 5, 5, 2, 4, 5 };
    int n = arr.length;
     
    System.out.print(findLargestNumber(arr, n) + "\n");
}
}
 
// This code is contributed by gauravrajput1


Python3
# Python3 code for the above problem
 
# Function to find the largest number
# whose frequency is equal to itself.
def findLargestNumber(arr, n):
    for i in range(n):
        arr[i] &= 0xFFFF;
        if (arr[i] <= n):
 
            # Adding 65536 to keep the
            # count of the current number
            arr[i] += 0x10000;
         
    for i in range(n - 1, 0, -1):
 
        # Right shifting by 16 bits
        # to find the count of the
        # number i
        if ((arr[i] >> 16) == i):
            return i + 1;
     
    return -1;
 
# Driver code
if __name__ == '__main__':
    arr = [ 3, 2, 5, 5, 2, 4, 5 ];
    n = len(arr);
 
    print(findLargestNumber(arr, n));
 
# This code is contributed by Rohit_ranjan


C#
// C# code for the above problem
using System;
 
class GFG{
 
// Function to find the largest number
// whose frequency is equal to itself.
static int findLargestNumber(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
    {
        arr[i] &= 0xFFFF;
        if (arr[i] <= n)
        {
             
            // Adding 65536 to keep the
            // count of the current number
            arr[i] += 0x10000;
        }
    }
 
    for(int i = n - 1; i > 0; --i)
    {
         
        // Right shifting by 16 bits
        // to find the count of the
        // number i
        if ((arr[i] >> 16) == i)
            return i + 1;
    }
    return -1;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 2, 5, 5, 2, 4, 5 };
    int n = arr.Length;
     
    Console.Write(findLargestNumber(arr, n) + "\n");
}
}
 
// This code is contributed by 29AjayKumar


输出:
2


时间复杂度: O(N)
辅助空间复杂度: O(N)
另一种方法:
注意:仅当给定数组中的数字小于65536(即2 16)时,此方法才有效。

  1. 在这里,使用输入数组存储计数。
  2. 由于值是有限的,因此只需将整数的前半部分(前16位)用于加计数65536即可保持计数。
  3. 在以相反顺序遍历数组时,请使用右移运算符(右移16位),并返回计数等于其自身的第一个数字。

下面是上述方法的实现:

C++

// C++ code for the above problem
 
#include 
using namespace std;
 
// Function to find the largest number
// whose frequency is equal to itself.
int findLargestNumber(vector& 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 arr
        = { 3, 2, 5, 5, 2, 4, 5 };
 
    cout << findLargestNumber(arr)
         << endl;
    return 0;
}

Java

// Java code for the above problem
class GFG{
 
// Function to find the largest number
// whose frequency is equal to itself.
static int findLargestNumber(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
    {
        arr[i] &= 0xFFFF;
        if (arr[i] <= n)
        {
             
            // Adding 65536 to keep the
            // count of the current number
            arr[i] += 0x10000;
        }
    }
 
    for(int i = n - 1; i > 0; --i)
    {
         
        // Right shifting by 16 bits
        // to find the count of the
        // number i
        if ((arr[i] >> 16) == i)
            return i + 1;
    }
    return -1;
}
 
// Driver code
public static void main(String[] args)
{
    int []arr = { 3, 2, 5, 5, 2, 4, 5 };
    int n = arr.length;
     
    System.out.print(findLargestNumber(arr, n) + "\n");
}
}
 
// This code is contributed by gauravrajput1

Python3

# Python3 code for the above problem
 
# Function to find the largest number
# whose frequency is equal to itself.
def findLargestNumber(arr, n):
    for i in range(n):
        arr[i] &= 0xFFFF;
        if (arr[i] <= n):
 
            # Adding 65536 to keep the
            # count of the current number
            arr[i] += 0x10000;
         
    for i in range(n - 1, 0, -1):
 
        # Right shifting by 16 bits
        # to find the count of the
        # number i
        if ((arr[i] >> 16) == i):
            return i + 1;
     
    return -1;
 
# Driver code
if __name__ == '__main__':
    arr = [ 3, 2, 5, 5, 2, 4, 5 ];
    n = len(arr);
 
    print(findLargestNumber(arr, n));
 
# This code is contributed by Rohit_ranjan

C#

// C# code for the above problem
using System;
 
class GFG{
 
// Function to find the largest number
// whose frequency is equal to itself.
static int findLargestNumber(int[] arr, int n)
{
    for(int i = 0; i < n; i++)
    {
        arr[i] &= 0xFFFF;
        if (arr[i] <= n)
        {
             
            // Adding 65536 to keep the
            // count of the current number
            arr[i] += 0x10000;
        }
    }
 
    for(int i = n - 1; i > 0; --i)
    {
         
        // Right shifting by 16 bits
        // to find the count of the
        // number i
        if ((arr[i] >> 16) == i)
            return i + 1;
    }
    return -1;
}
 
// Driver code
public static void Main(String[] args)
{
    int []arr = { 3, 2, 5, 5, 2, 4, 5 };
    int n = arr.Length;
     
    Console.Write(findLargestNumber(arr, n) + "\n");
}
}
 
// This code is contributed by 29AjayKumar
输出:
2


时间复杂度: O(N)
辅助空间复杂度: O(1)