Skip to content

Commit dc0eb83

Browse files
Merge pull request neetcode-gh#2817 from chetannada/ltc-1486-js
Create 1486-XOR-operation-in-an-array.js
2 parents ee2ecd8 + 3fd28c3 commit dc0eb83

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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy