Skip to content
New issue

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

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

Already on GitHub? # to your account

Single Number #48

Open
kokocan12 opened this issue Jun 30, 2022 · 0 comments
Open

Single Number #48

kokocan12 opened this issue Jun 30, 2022 · 0 comments

Comments

@kokocan12
Copy link
Owner

Problem

When an array is given, each element of array is integer.
Every element of array is appear twice, except for one.
Find that one.

Solution

Using bitwise, we can remove repeated one.
ex) 4 ^ 4 = 0, 1 ^ 4 ^ 4 = 1.

The code is below.

const singleNumber = function(nums) {
    let res = 0;
    
    nums.forEach(num => {
        res = num ^ res;
    });
    
    return res;
};
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

1 participant