HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Finding the frequency of an element in a sorted list of integers

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thefrequencyelementfindingsortedlistintegers

Problem

Input: Array of sorted integers and an element to check the frequency for

Output: Frequency of the element

public class FrequencyElement {
public static void main(String args[]){
    //System.out.println("Enter Input string");

    int input_array []= {2,3,4,25,99,99,19000};
    int element = 99;

    System.out.println(checkFreq(input_array,element));
}
static int checkFreq(int a[],int n){
    //returns the frequency of n in a

    int indexOfElement=elementExists(a,n);

    if(indexOfElement!=-1){
        int count =0;
        while(a[indexOfElement]==n){
            count++;
            indexOfElement++;
        }
        return count;
    }
    else
        return 0;

}
static int elementExists(int input[], int element){
    int lo=0;
    int high = input.length-1;

    while(loinput[mid] ){
            lo = mid+1;
        }
        else if(element < input[mid]){
            high= mid-1;
        }
        else if (lo !=mid)
            high = mid;
        else {
            System.out.println("Mid is: " + mid);
            return mid;
        }
    }
    return -1;
}
}

Solution

Reinventing the wheel

There is already a binary search available in JDK.

Bug

On the array {2,3,4,25,99,99,99,99,19000} your implementation returns 0. Removing the lines

else if (lo !=mid)
    high = mid;


from elementExists seems to fix the issue.

Printing from an algorithm

System.out.println("Mid is: " + mid);


It is not a good idea to print to stdout from an algorithm; at least not in the production code. Think about what happens if your friend uses your code: he gets overwhelmed with all the output + I/O is usually computationally expensive.

Summa summarum

All in all, I had this in mind:

import java.util.Arrays;

public class FrequencyElement {

    public static void main(String args[]) {
        int input_array[] = {2, 3, 4, 25, 99, 99, 19000};
        int element = 99;
        System.out.println(getElementFrequency(input_array, element));
    }

    static int getElementFrequency(final int array[], final int queryElement) {
        final int index = getElementIndex(array, queryElement);

        // If there is more than one query elements. Arrays.binarySearch does 
        // not guarantee that the index returned will point to the left most
        // occurrence.
        if (index = 0 && queryElement == array[index - offset]) {
            ++offset;
            ++count;
        }

        offset = 1;

        while (index + offset < array.length 
                && queryElement == array[index + offset]) {
            ++offset;
            ++count;
        }

        return count;
    }

    private static int getElementIndex(final int[] array, 
                                       final int queryElement) {
        return Arrays.binarySearch(array, queryElement);
    }
}


Hope that helps.

Code Snippets

else if (lo !=mid)
    high = mid;
System.out.println("Mid is: " + mid);
import java.util.Arrays;

public class FrequencyElement {

    public static void main(String args[]) {
        int input_array[] = {2, 3, 4, 25, 99, 99, 19000};
        int element = 99;
        System.out.println(getElementFrequency(input_array, element));
    }

    static int getElementFrequency(final int array[], final int queryElement) {
        final int index = getElementIndex(array, queryElement);

        // If there is more than one query elements. Arrays.binarySearch does 
        // not guarantee that the index returned will point to the left most
        // occurrence.
        if (index < 0) {
            return 0;
        }

        int count = 1; // Count array[index].
        int offset = 1;

        while (index - offset >= 0 && queryElement == array[index - offset]) {
            ++offset;
            ++count;
        }

        offset = 1;

        while (index + offset < array.length 
                && queryElement == array[index + offset]) {
            ++offset;
            ++count;
        }

        return count;
    }

    private static int getElementIndex(final int[] array, 
                                       final int queryElement) {
        return Arrays.binarySearch(array, queryElement);
    }
}

Context

StackExchange Code Review Q#133352, answer score: 6

Revisions (0)

No revisions yet.