Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

136. 只出现一次的数字 #48

Open
webVueBlog opened this issue Sep 4, 2022 · 0 comments
Open

136. 只出现一次的数字 #48

webVueBlog opened this issue Sep 4, 2022 · 0 comments

Comments

@webVueBlog
Copy link
Owner

136. 只出现一次的数字

Description

Difficulty: 简单

Related Topics: 位运算, 数组

给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。

说明:

你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗?

示例 1:

输入: [2,2,1]
输出: 1

示例 2:

输入: [4,1,2,1,2]
输出: 4

Solution

Language: JavaScript

/**
 * @param {number[]} nums
 * @return {number}
 */
// var singleNumber = function(nums) {
//     let ans = 0;
//     for(const num of nums) {
//         ans ^= num;
//     }
//     return ans;
// };

var singleNumber = function(nums) {
    return nums.reduce((pre, cur) => {
        return pre ^ cur;
    }, 0)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant