Ranga Reddy
Ranga Reddy

Reputation: 3086

Finding most frequency number in array

By using following I am able to find most frequently occurring integer in an array. But following code it will not work for few scenarios. How can I fix the code inside for loop? I want to enhance this approach only.

class FindingMostFrequencyOccur {

    public static void main(String args[]) {
        int A[] = { 1, 2, 3, 3, 1, 3, 1};
        int M = 3; // Maximum Number in Array
        int result = findFrequency(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];
    }
}

Can you we improve in this line

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;
                }

Upvotes: 0

Views: 119

Answers (2)

c0der
c0der

Reputation: 18812

To get the frequency of m in a use:

static int findFrequency(int m, int[] a) {
     return (int)IntStream.of(a).filter(i-> i==m).count();
}

To get a map of all frequencies in a use:

static Map<Integer, Integer> findFrequency(int[] a) {
     Map<Integer, Integer> m = new HashMap<>();
     IntStream.of(a).distinct().forEach(i->{
            m.put(i, findFrequency(i,a));
     });
     return m;
}

Upvotes: 2

Robo Mop
Robo Mop

Reputation: 3553

In this logic, you take each element of the array, and find the number of times it is repeated to the right of it, in the array. This is given by local_frequency. If this happens to be more than max_frequency, then this number at arr[i] is stored in number, and then max_frequency stores 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;
}

Upvotes: 0

Related Questions