# 56-II.数组中数字出现的次数II

### 一、 题目

在一个数组 nums 中除一个数字只出现一次之外，其他数字都出现了三次。请找出那个只出现一次的数字。

示例 1：

输入：nums = \[3,4,3,3]

输出：4

示例 2：

输入：nums = \[9,1,7,9,7,9,7]

输出：1

限制：

1 <= nums.length <= 10000

1 <= nums\[i] < 2^31

来源：力扣（LeetCode）

链接：<https://leetcode-cn.com/problems/shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii-lcof>

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

### 二、 解法

* 创建一个临时队列，遍历 nums
  * 数字 item 存在，下标为 lastIdx
    * 插入 item 到队列末端
    * 移除 lastIdx 下标元素
  * 不存在
    * 插入到队列头部
* 遍历完成，队列头部元素即为结果

```
func singleNumber(_ nums: [Int]) -> Int {
    var queue: [Int] = []
    
    for item in nums {
        if let lastIdx = queue.firstIndex(of: item) {
            queue.remove(at: lastIdx)
            queue.append(item)
        }
        else {
            queue.insert(item, at: 0)
        }
    }
    
    return queue[0]
}
```


---

# 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/56ii.-shu-zu-zhong-shu-zi-chu-xian-de-ci-shu-ii.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.
