Skip to content

Instantly share code, notes, and snippets.

@kninami
Created October 21, 2016 07:52
Show Gist options
  • Select an option

  • Save kninami/b6538ca933338d2b21dbbed1bd900eff to your computer and use it in GitHub Desktop.

Select an option

Save kninami/b6538ca933338d2b21dbbed1bd900eff to your computer and use it in GitHub Desktop.
function solution(A) {
let solver = {
makeCounterHash: (A) => {
let counterMap = {};
const ARR_LEN = A.length;
const HALF_IDX = Math.floor(ARR_LEN / 2);
for (let idx = 0; idx <= HALF_IDX; idx++) {
const prenum = A[idx];
if (counterMap.hasOwnProperty(prenum)) {
counterMap[prenum]++;
} else {
counterMap[prenum] = 1;
}
if (idx !== HALF_IDX) {
const postnum = A[ARR_LEN - idx - 1];
if (counterMap.hasOwnProperty(postnum)) {
counterMap[postnum]++;
} else {
counterMap[postnum] = 1;
}
}
}
return counterMap;
},
getOddNumFromCounterMap: (map) => {
for (let k in map) {
if (map[k] % 2 === 1) {
return k;
}
}
}
};
const counterMap = solver.makeCounterHash(A);
return Number.parseInt(solver.getOddNumFromCounterMap(counterMap));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment