###### tags: `Leetcode` # 数组 :::spoiler 只出现一次的数字 [LeetCode136](https://leetcode-cn.com/problems/single-number/) #### 解题思路 **解法1:用位运算->异或(xor)** 因为异或有三个特性: 1.0和任意数做异或等于任意数本身:0 xor a = a 2.相同两个数做异或等于0:a xor a = 0 3.符合交换律和结合律;也就是说多个数做异或,先以相同数做异或,再跟不同数做异或: a xor b xor a = a xor a xor b ### Kotlin ```kotlin= class Solution { fun singleNumber(nums: IntArray): Int { var r = 0 nums.forEach{ r = r xor it } return r } } ``` **时间复杂度:O(n)** **空间复杂度:O(1)** :::