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

### 一、 题目

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

示例 1:

输入: nums = \[5,7,7,8,8,10], target = 8

输出: 2

示例 2:

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

输出: 0   提示：

0 <= nums.length <= 105

-109 <= nums\[i] <= 109

nums 是一个非递减数组

-109 <= target <= 109

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi-lcof>

著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。

### 二、 解法

```
func search(_ nums: [Int], _ target: Int) -> Int {
    guard
        let first = nums.firstIndex(of: target),
        let last = nums.lastIndex(of: target)
    else {
        return 0
    }
    return last - first + 1
}
```


---

# 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://ryukiedev.gitbook.io/wiki/shu-ju-jie-gou-yu-suan-fa/jian-zhi-offerswift/53i.-zai-pai-xu-shu-zu-zhong-cha-zhao-shu-zi.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.
