# 排序数组中的出现次数

## 剑指 Offer 53 - I. 在排序数组中查找数字 I

### 题目

```javascript
统计一个数字在排序数组中出现的次数。



示例 1:

输入: nums = [5,7,7,8,8,10], target = 8
输出: 2
示例 2:

输入: nums = [5,7,7,8,8,10], target = 6
输出: 0


限制：

0 <= 数组长度 <= 50000
```

### 题解

#### 二分法

```javascript
/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 */
var search = function(nums, target) {
    return getRightMargin(nums,target)-getRightMargin(nums,target-1)
};

// 二分法查找目标数的右边界 和 目标数-1的右边界
const getRightMargin = function(nums,target){
    let _left = 0,
        _right = nums.length-1;

    while(_left<=_right){
        const _mid = (_left+_right)>>1;
        if(target >= nums[_mid]){
            _left = _mid + 1;
        }else{
            _right = _mid - 1;
        }
    }

    return _left;
}
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://voyz.gitbook.io/voyz-algorithm/shu-zu/pai-xu-shu-zu-zhong-de-chu-xian-ci-shu.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
