📌  相关文章
📜  给定数组中数字 K 的计数频率

📅  最后修改于: 2022-05-13 01:57:51.729000             🧑  作者: Mango

给定数组中数字 K 的计数频率

给定一个大小为N的整数数组arr[]和一个数字整数K。任务是找出数组中数字 K 出现的总次数

例子:

方法:这个想法是遍历数组,对于每个单独的数组元素,计算其中数字 K 的出现次数并更新总计数。请按照以下步骤解决问题:

  1. 将数字 K 的总出现次数(例如count )初始化为 0。
  2. start元素到 end遍历给定的数组。
  3. 对于每个遍历的元素,找到该元素中数字K频率
  4. 将计数添加总和中。
  5. 返回总和作为答案。

下面是上述方法的实现:

C++
// C++ code to count frequency
// of digit K in given Array
 
#include 
using namespace std;
 
// Function to count occurrences
// in an element
int countOccurrences(int num, int K)
{
    // If num is 0
    if (K == 0 && num == 0)
        return 1;
 
    // Initialize count
    int count = 0;
 
    // Count for occurrences of digit K
    while (num > 0) {
        if (num % 10 == K)
            count++;
        num /= 10;
    }
    return count;
}
 
// Function to return sum of
// total occurrences of digit K
int totalOccurrences(int arr[], int N, int K)
{
 
    // Initialize sum
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
        sum += countOccurrences(arr[i], K);
    }
 
    return sum;
}
 
// Driver Code
int main()
{
    int arr[] = { 15, 66, 26, 91 };
    int K = 6;
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    cout << totalOccurrences(arr, N, K);
    return 0;
}


Java
// Java program for the above approach
import java.io.*;
class GFG {
 
  // Function to count occurrences
  // in an element
  static int countOccurrences(int num, int K)
  {
 
    // If num is 0
    if (K == 0 && num == 0)
      return 1;
 
    // Initialize count
    int count = 0;
 
    // Count for occurrences of digit K
    while (num > 0) {
      if (num % 10 == K)
        count++;
      num /= 10;
    }
    return count;
  }
 
  // Function to return sum of
  // total occurrences of digit K
  static int totalOccurrences(int arr[], int N, int K)
  {
 
    // Initialize sum
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
      sum += countOccurrences(arr[i], K);
    }
 
    return sum;
  }
 
  // Driver Code
  public static void main (String[] args)
  {
     
    int arr[] = { 15, 66, 26, 91 };
    int K = 6;
    int N = arr.length;
    System.out.println( totalOccurrences(arr, N, K));
  }
}
 
// This code is contributed by Potta Lokesh


Python3
# Python3 code to count frequency
# of digit K in given array
 
# Function to count occurrences
# in an element
def countOccurrences(num, K):
 
    # If num is 0
    if (K == 0 and num == 0):
        return 1
 
    # Initialize count
    count = 0
 
    # Count for occurrences of digit K
    while (num > 0):
        if (num % 10 == K):
            count += 1
             
        num = (num // 10)
         
    return count
 
# Function to return sum of
# total occurrences of digit K
def totalOccurrences(arr, N, K):
 
    # Initialize sum
    sum = 0
 
    # Traverse the array
    for i in range(N):
        sum += countOccurrences(arr[i], K)
 
    return sum
 
# Driver Code
arr = [ 15, 66, 26, 91 ]
K = 6
 
N = len(arr)
 
print(totalOccurrences(arr, N, K))
 
# This code is contributed by saurabh_jaiswal.


C#
// C# program for the above approach
using System;
class GFG {
 
  // Function to count occurrences
  // in an element
  static int countOccurrences(int num, int K)
  {
 
    // If num is 0
    if (K == 0 && num == 0)
      return 1;
 
    // Initialize count
    int count = 0;
 
    // Count for occurrences of digit K
    while (num > 0) {
      if (num % 10 == K)
        count++;
      num /= 10;
    }
    return count;
  }
 
  // Function to return sum of
  // total occurrences of digit K
  static int totalOccurrences(int []arr, int N, int K)
  {
 
    // Initialize sum
    int sum = 0;
 
    // Traverse the array
    for (int i = 0; i < N; i++) {
      sum += countOccurrences(arr[i], K);
    }
 
    return sum;
  }
 
  // Driver Code
  public static void Main ()
  {
     
    int []arr = { 15, 66, 26, 91 };
    int K = 6;
    int N = arr.Length;
    Console.Write( totalOccurrences(arr, N, K));
  }
}
 
// This code is contributed by Samim Hossain Mondal.


Javascript


输出
3

时间复杂度: O(N*D) 其中 D 是数组元素中的最大位数
辅助空间: O(1)