Created
March 23, 2018 03:12
-
-
Save amorfati0310/e0537a68992299a5f2e68054b7840774 to your computer and use it in GitHub Desktop.
codiltity02-02.js
This file contains 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(arr) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
const reducer = (a, c, ci, ar) => { | |
console.log('asdasda', a, c, ci, ar); | |
if (a.length === 0) { | |
const filtered = ar.filter(el => el !== c) | |
return filtered; | |
} | |
if (a.length === 1) return a; | |
return a.filter(el => el !== c) | |
} | |
const result = arr.reduce(reducer, []) | |
return result[0]; | |
} | |
console.log(solution([1, 1, 1, 1, 1, 1, 1, 1, 1, 9])); | |
function solution2(arr) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
while (arr.length > 1) { | |
let filterItem = arr[0]; | |
arr = arr.filter(el => el !== filterItem) | |
} | |
return arr[0]; | |
} | |
function solution3(arr) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
let result = 0; | |
arr.forEach(element => { | |
if (arr.indexOf(element) === arr.lastIndexOf(element)) { | |
return result = element | |
} | |
}); | |
return result; | |
} | |
function solution4(arr) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
while (arr.length > 1) { | |
let filterItem = arr[0]; | |
arr = arr.filter(el => el !== filterItem) | |
} | |
return arr[0]; | |
} | |
function solution5(a) { | |
// write your code in JavaScript (Node.js 8.9.4) | |
const converted = a.reduce(function (obj, v) { | |
if (!obj[v]) obj[v] = 1; | |
else { | |
obj[v] = obj[v] + 1 | |
} | |
return obj; | |
}, {}) | |
for (key in converted) { | |
if (converted[key] === 1) { | |
return parseInt(key); | |
} | |
} | |
} | |
// |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment