查找数组中的大多数频率编号

问题描述 投票:0回答:2

通过使用以下命令,我能够找到数组中最频繁出现的整数。但是下面的代码在少数情况下不起作用。如何修复for循环中的代码?我只想增强此方法。

class FindingMostFrequencyOccur {

    public static void main(String args[]) {
        int A[] = { 1, 2, 3, 3, 1, 3, 1};
        int M = 3;
        int result = solution(M, A);
        System.out.println("Result "+result);
    }

    static int findFrequency(int M, int[] A) {
        int N = A.length;
        int[] count = new int[M + 1];
        for (int i = 0; i <= M; i++)
            count[i] = 0;
        int maxOccurence = 1;
        int index = -1;
        for (int i = 0; i < N; i++) {
            if (count[A[i]] > 0) {
                int tmp = count[A[i]];
                if (tmp > maxOccurence) {
                    maxOccurence = tmp;
                    index = i;
                }
                count[A[i]] = tmp + 1;
            } else {
                count[A[i]] = 1;
            }
        }
        return A[index];
    }
}
java arrays data-structures
2个回答
0
投票

在这种逻辑中,您获取数组的每个元素,并在数组的右侧找到重复的次数。这由local_frequency给出。如果恰好大于max_frequency,则此arr[i]处的数字将存储在number中,然后max_frequency将存储local_frequency

public static void main(String[] args)
{
    int[] arr = {1, 2, 3, 4, 3, 2, 1, 5, 5, 5, 4, 4, 3, 4};
    int result = findMostFrequent(arr);
    System.out.println(result + " is the most frequent number");
}

public static int findMostFrequent(int[] arr)
{
    int number = arr[0];
    int maxFrequency = 0;

    for(int i =0 ; i < arr.length; i++)
    {
        int local_frequency = 0;
        for(int j = i; j < arr.length; j++)
        {
            if(arr[i] == arr[j])
            local_frequency++;
        }

        if(local_frequency > maxFrequency)
        {
            number = arr[i];
            maxFrequency = local_frequency;
        }
    }

    return number;
}

0
投票

我认为一种有效的解决方案是使用哈希。我们创建一个哈希表,并将元素及其频率计数存储为键值对。最后,我们遍历哈希表并打印具有最大值的密钥。

 import java.util.HashMap; 
 import java.util.Map; 
 import java.util.Map.Entry; 

 class solution
    {
    static int mostFrequent(int arr[], int n) 
    { 

        Map<Integer, Integer> hp = 
               new HashMap<Integer, Integer>(); 

        for(int i = 0; i < n; i++) 
        { 
            int key = arr[i]; 
            if(hp.containsKey(key)) 
            { 
                int freq = hp.get(key); 
                freq++; 
                hp.put(key, freq); 
            } 
            else
            { 
                hp.put(key, 1); 
            } 
        } 

        // find max frequency. 
        int max_count = 0, res = -1; 

        for(Entry<Integer, Integer> val : hp.entrySet()) 
        { 
            if (max_count < val.getValue()) 
            { 
                res = val.getKey(); 
                max_count = val.getValue(); 
            } 
        } 

        return res; 
    }
    public static void main(String[] args) {
        int arr[] = {1, 5, 2, 1, 3, 2, 1}; 
        int n = arr.length; 

        System.out.println(mostFrequent(arr, n)); 
    }
 }

输出

1

时间复杂度:O(n)

但是如果您仅选择方法,则:

 public int getPopularElement(int[] a)
    {
    int count = 1, tempCount;
    int popular = a[0];
    int temp = 0;
    for (int i = 0; i < (a.length - 1); i++)
    {
       temp = a[i];
       tempCount = 0;
    for (int j = 1; j < a.length; j++)
    {
    if (temp == a[j])
       tempCount++;
    }
    if (tempCount > count)
    {
      popular = temp;
      count = tempCount;
    }
    }
    return popular;
 }

0
投票

使用IntStream

static int findFrequency(int m, int[] a) {
     return (int)IntStream.of(a).filter(i-> i==m).count();
}
© www.soinside.com 2019 - 2024. All rights reserved.