Write a javascript functionGiven an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times.
时间: 2024-01-03 09:06:05 浏览: 84
Given an array of integers, return indices of the two numbers
Sure, here's a JavaScript function that solves the problem you described:
```javascript
function findOdd(arr) {
let result = 0;
for (let i = 0; i < arr.length; i++) {
result ^= arr[i];
}
return result;
}
```
This function uses the XOR (^) operator to find the integer that appears an odd number of times. The XOR operator returns a 1 in each bit position for which the corresponding bits of either but not both operands are 1s. By XORing all the integers in the array together, we end up with the integer that appears an odd number of times.
阅读全文