Created
April 1, 2020 18:27
-
-
Save varatep/26ebf3b0ad3a0662ab4a132b413da092 to your computer and use it in GitHub Desktop.
leetcode 30-day week 1 day 1
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
/** | |
* @param {number[]} nums | |
* @return {number} | |
*/ | |
var singleNumber = function(nums) { | |
let hash = {}; | |
for (let i = 0; i < nums.length; i++) { | |
if (typeof hash[nums[i]] === 'undefined') { | |
hash[nums[i]] = 1; | |
} else { | |
hash[nums[i]] = hash[nums[i]] + 1; | |
} | |
} | |
const keys = Object.keys(hash); | |
for (let i = 0; i < keys.length; i++) { | |
if (hash[keys[i]] === 1) return keys[i]; | |
} | |
return nums[0]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment