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

📅  最后修改于: 2021-06-26 22:24:49             🧑  作者: 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
?>


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

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

如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。