Java Simple neat and clean code


#1

public class Solution {

public int findCount(final int[] A, int B) {
    int n = A.length;
    int ind = binarySearch(A, 0,n - 1, B);

    if (ind == -1)
        return 0;

    int count = 1;
    int left = ind - 1;
    while (left >= 0 &&
           A[left] == B)
    {
        count++;
        left--;
    }

    int right = ind + 1;
    while (right < n &&
           A[right] == B)
    {
        count++;
        right++;
    }

    return count;
}
int binarySearch(int arr[], int l,
                        int r, int x)
{
    if (r < l)
        return -1;

    int mid = l + (r - l) / 2;

    if (arr[mid] == x)
        return mid;

    if (arr[mid] > x)
        return binarySearch(arr, l,
                            mid - 1, x);

    return binarySearch(arr, mid + 1, r, x);
}

}