// 方法一
import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Solution {
    public int GetNumberOfK(int [] array , int k) {
       if(array == null || array.length == 0 || k > array[array.length - 1] || k < array[0]){
           return 0;
       }

       return Arrays.stream(array)
           .boxed()
           .collect(Collectors.toMap(e->e,e->1,Integer::sum))
           .getOrDefault(k,0);
    }
}
// 方法二
import java.io.*;
import java.util.*;
import java.util.stream.*;

public class Solution {
    public int GetNumberOfK(int [] array , int k) {

        if(array == null || array.length == 0 || array[array.length - 1] < k || k < array[0]){
            return 0;
        }

        int pos1 = 0;
        int left = 0, right = array.length - 1;
        while(left + 1 < right){
            int mid = left + (right - left)/2;
            if(array[mid] >= k){
                right = mid;
            }else{
                left = mid;
            }
        }

        if(array[left] == k){
            pos1 = left;
        }else if(array[right] == k){
            pos1 = right;
        }else{
            return 0;
        }

        int pos2 = 0;
        left = 0;
        right = array.length - 1;
        while(left + 1 < right){
            int mid = left + (right - left)/2;
            if(array[mid] <= k){
                left = mid;
            }else{
                right = mid;
            }
        }

        if(array[right] == k){
            pos2 = right;
        }else if(array[left] == k){
            pos2 = left;
        }

        return pos2 - pos1 + 1;
    }
}