📌  相关文章
📜  小范围值数组中每个元素的频率

📅  最后修改于: 2021-10-28 01:25:58             🧑  作者: Mango

给定一个数组,其中元素在小范围内。数组中的最大元素不超过数组的大小。查找元素的频率。
例子:

Input : arr[] = {3, 1, 2, 3, 4, 5, 4}
Output: 1-->1
        2-->1
        3-->2
        4-->2
        5-->1

Input : arr[] = {1, 2, 2, 1, 2}
Output: 1-->2
        2-->3

Input : arr[] = {1, 2, 4}
Output: 1-->1
        2-->1
        4-->1

一个简单的解决方案是使用两个嵌套循环。对于每个元素(从 1 到 n,其中 n 是数组的大小),计算它出现的次数。此解决方案的时间复杂度为 O(n*n)
更好的解决方案是使用排序。先对数组进行排序,排序后,线性遍历数组,统计每个元素出现的次数。此解决方案的时间复杂度为 O(n Log n)
一个有效的解决方案是使用散列。我们将每个元素插入哈希表并增加频率。该解决方案的时间复杂度为 O(n)。请参阅用于竞争性编程的频率测量技术以进行实施。
有限范围的有效解决方案
基于散列的解决方案很快,但需要散列函数计算等。如果我们知道范围很小,我们使用直接地址表,在其中创建一个大小等于最大值的数组并使用数组元素作为索引。
以下是上述解释的实现:

C++
// CPP program to find frequencies of elements in
// limited range array.
#include 
using namespace std;
 
void frequencyOfEach(int* arr, int n)
{
    // finding maximum element in array
    int max = *max_element(arr, arr + n);
 
    // make hash array of size equal to maximum
    // element in array
    int hash[max + 1] = { 0 };
 
    /* Counting frequency of each element of array
       and storing it in hash*/
    for (int i = 0; i < n; i++) {
        hash[arr[i]]++;
    }
 
    // printing frequency of elements
    for (int i = 0; i <= max; i++) {
 
        /* If hash[i] has stored any value
           i.e element has occurred atleat
           once in array */
        if (hash[i] != 0)
            cout << i << "-->" << hash[i] << "\n";
    }
}
 
int main()
{
    int arr[] = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 };
    int n = sizeof(arr) / sizeof(arr[0]);
    frequencyOfEach(arr, n);
    return 0;
}


Java
// Java program to find frequencies of elements in
// limited range array.
import java.util.*;
 
class solution
{
 
static void frequencyOfEach(int []arr, int n)
{
    int max = Integer.MIN_VALUE;
    // finding maximum element in array
    for (int i = 0;imax)
        max = arr[i];
}
     
 
    // make hash array of size equal to maximum
    // element in array
    int []hash = new int[max + 1];
    Arrays.fill(hash,0);
 
    /* Counting frequency of each element of array
    and storing it in hash*/
    for (int i = 0; i < n; i++) {
        hash[arr[i]]++;
    }
 
    // printing frequency of elements
    for (int i = 0; i <= max; i++) {
 
        /* If hash[i] has stored any value
        i.e element has occurred atleat
        once in array */
        if (hash[i] != 0)
            System.out.println(i+"-->"+hash[i]);
    }
}
 
public static void main(String args[])
{
    int []arr = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 };
    int n = arr.length;
    frequencyOfEach(arr, n);
     
}
}
//This code is contributed by Surendra_Gangwar


Python3
# Python 3 program to find frequencies
# of elements in limited range array.
def frequencyOfEach(arr, n) :
     
    # finding maximum element in array
    max_element = max(arr)
 
    # make hash array of size equal
    # to maximum element in array
    hash = [0] * (max_element + 1)
 
    # Counting frequency of each element
    # of array and storing it in hash
    for i in range(n) :
        hash[arr[i]] += 1
     
    # printing frequency of elements
    for i in range(max_element + 1) :
 
        # If hash[i] has stored any value
        # i.e element has occurred atleat
        # once in array
        if (hash[i] != 0) :
            print(i, "-->", hash[i])
     
# Driver Code
if __name__ == "__main__" :
 
    arr = [ 5, 2, 2, 3, 5,
            1, 1, 5, 3, 4 ]
    n = len(arr)
    frequencyOfEach(arr, n);
 
# This code is contributed by Ryuga


C#
// C# program to find frequencies of elements in
// limited range array.
using System;
  
public class solution{
 
    static void frequencyOfEach(int []arr, int n)
    {
        int max = int.MinValue;
        // finding maximum element in array
        for (int i = 0;imax)
            max = arr[i];
    }
 
 
        // make hash array of size equal to maximum
        // element in array
        int []hash = new int[max + 1];
 
        /* Counting frequency of each element of array
        and storing it in hash*/
        for (int i = 0; i < n; i++) {
            hash[arr[i]]++;
        }
 
        // printing frequency of elements
        for (int i = 0; i <= max; i++) {
 
            /* If hash[i] has stored any value
            i.e element has occurred atleat
            once in array */
            if (hash[i] != 0)
                Console.WriteLine (i+"-->"+hash[i]);
        }
    }
 
    public static void Main()
    {
        int []arr = { 5, 2, 2, 3, 5, 1, 1, 5, 3, 4 };
        int n = arr.Length;
        frequencyOfEach(arr, n);
 
    }
}
// This code is contributed by PrinciRaj1992


PHP
" . $hash[$i] . "\n";
    }
}
 
// Driver Code
$arr = array(5, 2, 2, 3, 5, 1, 1, 5, 3, 4 );
$n = sizeof($arr);
frequencyOfEach($arr, $n);
 
// This code is contributed by ita_c
?>


Javascript


输出:
1-->2
2-->2
3-->2
4-->1
5-->3

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