Created
October 21, 2016 07:52
-
-
Save kninami/b6538ca933338d2b21dbbed1bd900eff to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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