1720. Decode XORed Array
문제
0이상의 n으로 이루어진 숨겨진 배열 arr가 존재하며 길이가 n-1인 encoded[i]=arr[i] XOR arr[i+1]인 배열 encoded와 arr[0]인 first가 매개변수로 주어질 때, 숨겨진 arr를 return하는 함수 만들기
가정
1. arr의 길이는 2이상 10,000이하
2. encoded 길이는 arr의길이 -1
3. encoded의 모든 원소는 0이상 100,000이하의 정수
4. first는 0이상 100,000이하의 정수
풀이
var decode = function(encoded, first) {
const result = new Array(encoded.length).fill(0);
result[0]=first;
for(let i = 0; i<encoded.length; i++){
result[i+1]=encoded[i]^result[i];
}
return result;
};
xor
The bitwise XOR operator (^) returns a 1 in each bit position
for which the corresponding bits of either but not both operands
are 1s.