Skip to content

[clara-shin] WEEK11 Solutions #1563

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions missing-number/clara-shin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* n개의 서로 다른 숫자가 들어있는 배열에서, 0부터 n까지의 범위 중 빠진 숫자 하나를 찾는 문제
* 배열의 길이가 n이면, 실제로는 0부터 n까지 총 (n+1)개의 숫자 중 하나가 빠져있음
*
* 접근방법: XOR 비트 연산
* 시간 복잡도: O(n)
* 공간 복잡도: O(1)
*
* XOR의 성질:
* - a ^ a = 0 (같은 수끼리 XOR하면 0)
* - a ^ 0 = a (어떤 수든 0과 XOR하면 자기 자신)
* - XOR은 교환법칙과 결합법칙이 성립
*/

/**
* @param {number[]} nums
* @return {number}
*/
var missingNumber = function (nums) {
let result = nums.length; // n으로 초기화

// 인덱스 i와 nums[i]를 모두 XOR
// 결국 빠진 숫자만 홀수 번 나타나서 결과로 남음
for (let i = 0; i < nums.length; i++) {
result ^= i ^ nums[i];
}

return result;
};