Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // array has numbers that appear twice, but not necessarily in order
- // except one number will not appear twice
- const nums = [0, 0, 1, 1, 2, 2, 3, 4, 4, 5, 5];
- // faster
- const getSingleNum = nums => {
- let pairs = {};
- nums.forEach(num =>
- pairs[num]
- ? (delete pairs[num])
- : (pairs[num] = true));
- return Object.keys(pairs)[0];
- }
- // slower
- const getSingleNumOther = nums =>
- nums.find((num, i) =>
- !nums.some((otherNum, j) =>
- i !== j && num === otherNum
- )
- );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement