param { number [ ] } numbers param { number } k return { number } function GetNumberOfK ( numbers , k ) { if ( ! numbers || numbers . length === 0 ) { return 0 ; } let first = GetFirstK ( numbers , k ) ; let last = GetLastK ( numbers , k ) ; if ( first !== - 1 && last !== - 1 ) { return last - first + 1 ; } else { return 0 ; } } param { number [ ] } numbers param { number } k return { number } function GetFirstK ( numbers , k ) { if ( ! numbers || numbers . length === 0 ) { return - 1 ; } let left = 0 ; let right = numbers . length - 1 ; while ( left <= right ) { let mid = Math . floor ( left + ( right - left ) / 2 ) ; if ( numbers [ mid ] === k ) { if ( ( mid > 0 && numbers [ mid - 1 ] !== k ) || mid === 0 ) { return mid ; } else { right = mid - 1 ; } } else if ( numbers [ mid ] > k ) { right = mid - 1 ; } else { left = mid + 1 ; } } return - 1 ; } param { number [ ] } numbers param { number } k return { number } function GetLastK ( numbers , k ) { if ( ! numbers || numbers . length === 0 ) { return - 1 ; } let len = numbers . length ; let left = 0 ; let right = len - 1 ; while ( left <= right ) { let mid = Math . floor ( left + ( right - left ) / 2 ) ; if ( numbers [ mid ] === k ) { if ( ( mid < len - 1 && numbers [ mid + 1 ] !== k ) || mid === len - 1 ) { return mid ; } else { left = mid + 1 ; } } else if ( numbers [ mid ] < k ) { left = mid + 1 ; } else { right = mid - 1 ; } } return - 1 ; } export { GetNumberOfK } ;