Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

js array value that appears odd number of times

// Find the odd int
function findOdd(A) {
  return A.reduce((a, b) => a ^ b);
}

console.log(findOdd([20,1,-1,2,-2,3,3,5,5,1,2,4,20,4,-1,-2,5], 5)); // 5
console.log(findOdd([1,1,2,-2,5,2,4,4,-1,-2,5], -1)); // -1
console.log(findOdd([20,1,1,2,2,3,3,5,5,4,20,4,5], 5)); // 5
Comment

check if number appears odd number of times in array javascript

function findOdd(A) {
    let counts = A.reduce((p, n) => (p[n] = ++p[n] || 1, p), {});
    return +Object.keys(counts).find(k => counts[k] % 2) || undefined;
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: in which language python is written 
Javascript :: react native metro api level 30 
Javascript :: js input validate excel file type 
Javascript :: js on edge files 
Javascript :: Cannot call `JSON.parse` with item bound to `text` because null or undefined [1] is incompatible with string 
Javascript :: regex exec fails twice 
Javascript :: adonisjs column default value 
Javascript :: react native monorepo module resolver outside app 
Javascript :: clean up async requests in react useEffect hook using abort controller 
Javascript :: check radio button is checked jquery 
Javascript :: set storage react 
Javascript :: discord.js join voice channel 
Javascript :: how to use componentdidmount in functional component 
Javascript :: javascript button onclick 
Javascript :: javascript opacity 
Javascript :: javascript array remove element 
Javascript :: Sort an array using setTimeout function 
Javascript :: how to make something spawn randomly p5.js 
Javascript :: react native navigation tabBarButton is focused 
Javascript :: current time in javascript 
Javascript :: remove sliding animation from owl carousel 
Javascript :: javascript tofixed only if decimal 
Javascript :: email regular expression 
Javascript :: is java and javascript a good combo 
Javascript :: how to get the contract address from the contract instance web3js 
Javascript :: convert hsl to hex code javascript 
Javascript :: reactdom.render is no longer supported in react 18 
Javascript :: queryselector attribute 
Javascript :: modulo do angular httpclient 
Javascript :: usestate in object 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =