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 :: socket.io cors 
Javascript :: photo in React native 
Javascript :: onload of modal jquery function 
Javascript :: hammer js cdn 
Javascript :: hide_node example jstree 
Javascript :: javascript href on load delay 
Javascript :: jest listen EADDRINUSE: address already in use :::5000 jest 
Javascript :: javascript get image dimensions 
Javascript :: get random element from string array java 
Javascript :: js how to sort strings in array 
Javascript :: how get height elemnt with that margin in js 
Javascript :: install bootstrap in react 
Javascript :: javascript cast string to float 
Javascript :: dropzone csrf codeigniter 
Javascript :: how to enable javascript 
Javascript :: filter json array by key in angular 9 
Javascript :: momentjs utcoffset 
Javascript :: npm sendgrid 
Javascript :: return promise in node js 
Javascript :: angular get device information 
Javascript :: call ajax after ajax 
Javascript :: react router dom change default path 
Javascript :: Access child elements of a main element js 
Javascript :: node.js copy to clipboard 
Javascript :: first N elements of an array javascript 
Javascript :: icon shwoing a box react native vector icons 
Javascript :: javascript change title 
Javascript :: document.queryselector null check 
Javascript :: how to delay something in javascript 
Javascript :: Get element id by name 
ADD CONTENT
Topic
Content
Source link
Name
3+4 =