Finding the frequency of an element in a sorted list of integers 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(lo&lt;high){
        int mid = (lo + high )/2;
        if(element &gt;input[mid] ){
            lo = mid+1;
        }
        else if(element &lt; input[mid]){
            high= mid-1;
        }
        else if (lo !=mid)
            high = mid;
        else {
            System.out.println("Mid is: " + mid);
            return mid;
        }
    }
    return -1;
}
}
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 &lt; 0) {
            return 0;
        }

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

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

        offset = 1;

        while (index + offset &lt; array.length 
                &amp;&amp; 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.