📌  相关文章
📜  查找排序数组中每个元素的频率

📅  最后修改于: 2021-10-27 08:38:32             🧑  作者: Mango

给定一个由N 个整数组成的排序数组arr[] ,任务是找到每个数组元素的频率。

例子:

朴素的方法:最简单的方法是遍历数组并保留在 HashMap 中遇到的每个元素的计数,然后最后通过遍历 HashMap 打印每个元素的频率。这种方法已经在这里实施。

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

高效的方法:上述方法可以在空间使用方面进行优化,因为在排序数组中,相同的元素是连续出现的,所以想法是在遍历数组时维护一个变量来跟踪元素的频率大批。请按照以下步骤解决问题:

  • 初始化一个变量,比如freq1来存储元素的频率。
  • 使用变量i在范围[1, N-1] 中迭代并执行以下步骤:
    • 如果arr[i] 的值等于arr[i-1] ,则将freq增加1
    • 否则打印值在freq 中获得的arr[i-1]频率,然后将freq更新为1
  • 最后,在上述步骤之后,将数组的最后一个不同元素的频率打印为freq

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
    // Function to print the frequency
    // of each element of the sorted array
     void printFreq(vector &arr, int N)
    {
        
        // Stores the frequency of an element
        int freq = 1;
        
       // Traverse the array arr[]
        for (int i = 1; i < N; i++)
        {
           
            // If the current element is equal
            // to the previous element
            if (arr[i] == arr[i - 1])
            {
               
                // Increment the freq by 1
                freq++;
            }
           
        // Otherwise,
            else {
                cout<<"Frequency of "< arr
            = { 1, 1, 1, 2, 3, 3, 5, 5,
                8, 8, 8, 9, 9, 10 };
        int N = arr.size();
       
        // Function Call
        printFreq(arr, N);
    return 0;
    }   
 
// This code is contributed by codersaty


Java
// Java program for the above approach
import java.io.*;
import java.lang.*;
import java.util.*;
 
class GFG {
 
    // Function to print the frequency
    // of each element of the sorted array
    static void printFreq(int arr[], int N)
    {
 
        // Stores the frequency of an element
        int freq = 1;
 
        // Traverse the array arr[]
        for (int i = 1; i < N; i++) {
            // If the current element is equal
            // to the previous element
            if (arr[i] == arr[i - 1]) {
                // Increment the freq by 1
                freq++;
            }
 
            // Otherwise,
            else {
                System.out.println("Frequency of "
                                   + arr[i - 1]
                                   + " is: " + freq);
                // Update freq
                freq = 1;
            }
        }
 
        // Print the frequency of the last element
        System.out.println("Frequency of "
                           + arr[N - 1]
                           + " is: " + freq);
    }
 
    // Driver Code
    public static void main(String args[])
    {
        // Given Input
        int arr[]
            = { 1, 1, 1, 2, 3, 3, 5, 5,
                8, 8, 8, 9, 9, 10 };
        int N = arr.length;
 
        // Function Call
        printFreq(arr, N);
    }
}


Python3
# Python3 program for the above approach
 
# Function to print the frequency
# of each element of the sorted array
def printFreq(arr, N):
   
    # Stores the frequency of an element
    freq = 1
 
    # Traverse the array arr[]
    for i in range(1, N, 1):
       
        # If the current element is equal
        # to the previous element
        if (arr[i] == arr[i - 1]):
           
            # Increment the freq by 1
            freq += 1
 
        # Otherwise,
        else:
            print("Frequency of",arr[i - 1],"is:",freq)
                # Update freq
            freq = 1
 
    # Print the frequency of the last element
    print("Frequency of",arr[N - 1],"is:",freq)
 
# Driver Code
if __name__ == '__main__':
   
    # Given Input
    arr = [1, 1, 1, 2, 3, 3, 5, 5,8, 8, 8, 9, 9, 10]
    N = len(arr)
 
    # Function Call
    printFreq(arr, N)
     
    # This code is contributed by ipg2016107.


C#
// C# program for the above approach
using System;
 
public class GFG{
     
// Function to print the frequency
// of each element of the sorted array
static void printFreq(int[] arr, int N)
{
 
    // Stores the frequency of an element
    int freq = 1;
 
    // Traverse the array arr[]
    for (int i = 1; i < N; i++)
    {
 
      // If the current element is equal
      // to the previous element
      if (arr[i] == arr[i - 1])
      {
 
        // Increment the freq by 1
        freq++;
      }
 
      // Otherwise,
      else {
        Console.WriteLine("Frequency of " + arr[i - 1] + " is: " + freq);
        // Update freq
        freq = 1;
      }
    }
 
    // Print the frequency of the last element
    Console.WriteLine("Frequency of " + arr[N - 1] + " is: " + freq);
}
 
// Driver Code
static public void Main (){
 
    // Given Input
    int[] arr = { 1, 1, 1, 2, 3, 3, 5, 5,
                  8, 8, 8, 9, 9, 10 };
    int N = arr.Length;
       
    // Function Call
    printFreq(arr, N);
}
}
 
// This code is contributed by Dharanendra L V.


Javascript


输出
Frequency of 1 is: 3
Frequency of 2 is: 1
Frequency of 3 is: 2
Frequency of 5 is: 2
Frequency of 8 is: 3
Frequency of 9 is: 2
Frequency of 10 is: 1

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

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程