📜  查找数组中数字的频率

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

查找数组中数字的频率

给定一个数组 a[] 和一个元素 x,找出 x 在 a[] 中出现的次数。
例子:

Input  : a[] = {0, 5, 5, 5, 4}
           x = 5
Output : 3

Input  : a[] = {1, 2, 3}
          x = 4
Output : 0

如果数组未排序

这个想法很简单,我们将 count 初始化为 0。我们以线性方式遍历数组。对于与 x 匹配的每个元素,我们增加计数。最后,我们返回计数。
下面是该方法的实现。

C++
// CPP program to count occurrences of an
// element in an unsorted array
#include
using namespace std;
 
int frequency(int a[], int n, int x)
{
    int count = 0;
    for (int i=0; i < n; i++)
       if (a[i] == x)
          count++;
    return count;
}
 
// Driver program
int main() {
    int a[] = {0, 5, 5, 5, 4};
    int x = 5;
    int n = sizeof(a)/sizeof(a[0]);
    cout << frequency(a, n, x);
    return 0;
}


Java
// Java program to count
// occurrences of an
// element in an unsorted
// array
 
import java.io.*;
 
class GFG {
     
    static int frequency(int a[],
    int n, int x)
    {
        int count = 0;
        for (int i=0; i < n; i++)
        if (a[i] == x)
            count++;
        return count;
    }
     
    // Driver program
    public static void main (String[]
    args) {
         
        int a[] = {0, 5, 5, 5, 4};
        int x = 5;
        int n = a.length;
         
        System.out.println(frequency(a, n, x));
    }
}
 
// This code is contributed
// by Ansu Kumari


Python3
# Python program to count
# occurrences of an
# element in an unsorted
# array
def frequency(a, x):
    count = 0
     
    for i in a:
        if i == x: count += 1
    return count
 
# Driver program
a = [0, 5, 5, 5, 4]
x = 5
print(frequency(a, x))
 
# This code is contributed by Ansu Kumari


C#
// C# program to count
// occurrences of an
// element in an unsorted
// array
using System;
 
class GFG {
     
    static int frequency(int []a,
    int n, int x)
    {
        int count = 0;
        for (int i=0; i < n; i++)
        if (a[i] == x)
            count++;
        return count;
    }
     
    // Driver program
    static public void Main (){
     
         
        int []a = {0, 5, 5, 5, 4};
        int x = 5;
        int n = a.Length;
         
        Console.Write(frequency(a, n, x));
    }
}
 
// This code is contributed
// by Anuj_67


PHP


Javascript


CPP
// CPP program to answer queries for frequencies
// in O(1) time.
#include 
using namespace std;
  
unordered_map hm;
 
void countFreq(int a[], int n)
{
    // Insert elements and their
    // frequencies in hash map.
    for (int i=0; i


Java
// Java program to answer
// queries for frequencies
// in O(1) time.
 
import java.io.*;
import java.util.*;
 
class GFG {
     
   static HashMap  hm = new HashMap();
 
   static void countFreq(int a[], int n)
   {
        // Insert elements and their
        // frequencies in hash map.
        for (int i=0; i


Python3
# Python program to
# answer queries for
# frequencies
# in O(1) time.
 
hm = {}
 
def countFreq(a):
    global hm
     
    # Insert elements and their
    # frequencies in hash map.
     
    for i in a:
        if i in hm: hm[i] += 1
        else: hm[i] = 1
 
# Return frequency
# of x (Assumes that
# countFreq() is
# called before)
def query(x):
    if x in hm:
        return hm[x]
    return 0
 
# Driver program
a = [1, 3, 2, 4, 2, 1]
countFreq(a)
print(query(2))
print(query(3))
print(query(5))
 
# This code is contributed
# by Ansu Kumari


C#
// C# program to answer
// queries for frequencies
// in O(1) time.
using System;
using System.Collections.Generic;
class GFG
{
     
static Dictionary  hm = new Dictionary();
 
static void countFreq(int []a, int n)
{
    // Insert elements and their
    // frequencies in hash map.
    for (int i = 0; i < n; i++)
        if (hm.ContainsKey(a[i]) )
            hm[a[i]] = hm[a[i]] + 1;
        else hm.Add(a[i], 1);
}
     
// Return frequency of x (Assumes that
// countFreq() is called before)
static int query(int x)
{
    if (hm.ContainsKey(x))
        return hm[x];
    return 0;
}
     
// Driver code
public static void Main(String[] args)
{
    int []a = {1, 3, 2, 4, 2, 1};
    int n = a.Length;
    countFreq(a, n);
    Console.WriteLine(query(2));
    Console.WriteLine(query(3));
    Console.WriteLine(query(5));
}
}
 
// This code is contributed by 29AjayKumar


Javascript


输出:

3

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

如果数组已排序

我们可以将方法应用于已排序和未排序。但是对于排序数组,我们可以使用二分搜索优化它以在 O(Log n) 时间内工作。有关详细信息,请参阅下面的文章。计算排序数组中出现的次数(或频率)。

如果单个数组上有多个查询

我们可以使用散列来存储所有元素的频率。然后我们可以在 O(1) 时间内回答所有查询。有关详细信息,请参阅未排序数组中每个元素的频率。

CPP

// CPP program to answer queries for frequencies
// in O(1) time.
#include 
using namespace std;
  
unordered_map hm;
 
void countFreq(int a[], int n)
{
    // Insert elements and their
    // frequencies in hash map.
    for (int i=0; i

Java

// Java program to answer
// queries for frequencies
// in O(1) time.
 
import java.io.*;
import java.util.*;
 
class GFG {
     
   static HashMap  hm = new HashMap();
 
   static void countFreq(int a[], int n)
   {
        // Insert elements and their
        // frequencies in hash map.
        for (int i=0; i

Python3

# Python program to
# answer queries for
# frequencies
# in O(1) time.
 
hm = {}
 
def countFreq(a):
    global hm
     
    # Insert elements and their
    # frequencies in hash map.
     
    for i in a:
        if i in hm: hm[i] += 1
        else: hm[i] = 1
 
# Return frequency
# of x (Assumes that
# countFreq() is
# called before)
def query(x):
    if x in hm:
        return hm[x]
    return 0
 
# Driver program
a = [1, 3, 2, 4, 2, 1]
countFreq(a)
print(query(2))
print(query(3))
print(query(5))
 
# This code is contributed
# by Ansu Kumari

C#

// C# program to answer
// queries for frequencies
// in O(1) time.
using System;
using System.Collections.Generic;
class GFG
{
     
static Dictionary  hm = new Dictionary();
 
static void countFreq(int []a, int n)
{
    // Insert elements and their
    // frequencies in hash map.
    for (int i = 0; i < n; i++)
        if (hm.ContainsKey(a[i]) )
            hm[a[i]] = hm[a[i]] + 1;
        else hm.Add(a[i], 1);
}
     
// Return frequency of x (Assumes that
// countFreq() is called before)
static int query(int x)
{
    if (hm.ContainsKey(x))
        return hm[x];
    return 0;
}
     
// Driver code
public static void Main(String[] args)
{
    int []a = {1, 3, 2, 4, 2, 1};
    int n = a.Length;
    countFreq(a, n);
    Console.WriteLine(query(2));
    Console.WriteLine(query(3));
    Console.WriteLine(query(5));
}
}
 
// This code is contributed by 29AjayKumar

Javascript


输出 :

2
1
0