Skip to content

Commit 3fd28c3

Browse files
authored
Update 1486-XOR-operation-in-an-array.js
Added the Code for 1486-XOR-operation-in-an-array.js
1 parent 80aa90f commit 3fd28c3

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/**
2+
* @param {number} n
3+
* @param {number} start
4+
* @return {number}
5+
*/
6+
var xorOperation = function(n, start) {
7+
let nums = new Array(n).fill(0); // initialize a nums which is the length of n elements with 0 value of Array using Array() nad fill()
8+
9+
for(let i=0; i<n; i++){ // loop through the 0 to n
10+
nums[i] = start + (2 * i); // current element of nums is equal to sum of start and twice the i
11+
}
12+
13+
return nums.reduce((a,b) => a^b); // return the bitwise XOR of all elements of nums using reduce()
14+
};

0 commit comments

Comments
 (0)