- Notifications
You must be signed in to change notification settings - Fork2.4k
Refactor JS - Array & Hash#426
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Adding multiple solutions is being discussed in#189, It's also difficult to decide which refactors make the code easier to understand, even though I am partial to your use of array methods.
I don't know where@neetcode-gh wants to take this.
} | ||
}; | ||
return [ -1, -1 ] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
I don't thinkreturn [-1, -1]
is needed since"You may assume that each input would haveexactly one solution" for this problem.
/** | ||
* @param {number[]} nums | ||
* Time O(N) | Space O(N) | ||
* @return {boolean} | ||
*/ | ||
var containsDuplicate = function(nums) { | ||
const numsSet = new Set() | ||
for(const i of nums){ | ||
if(numsSet.has(i)){ | ||
return true | ||
} | ||
numsSet.add(i) | ||
} | ||
return false | ||
return (new Set(nums)).size !== nums.length |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
This solution has already been proposed (#371)
var isAnagram = function(s, t) { | ||
let map = {}; | ||
if (s.length !== t.length) return false; | ||
const reOrder = (str) => str | ||
.split('') | ||
.sort((a, b) => a.localeCompare(b)) | ||
.join(''); | ||
return reOrder(s) === reOrder(t) | ||
}; | ||
/** | ||
* @param {string} s | ||
* @param {string} t | ||
* Time O(N) | Space O(N) | ||
* @return {boolean} | ||
*/ | ||
var isAnagram = function(s, t, map = new Map()) { | ||
if (s.length !== t.length) return false; | ||
if (s.length !== t.length) { | ||
return false; | ||
} | ||
addCharFrequency(s, map); | ||
return subtractCharFrequency(t, map) | ||
for (let i = 0; i < s.length; i++) { | ||
if (map[s[i]]) { | ||
map[s[i]]++; | ||
} else { | ||
map[s[i]] = 1; | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others.Learn more.
Adding multiple solutions is being discussed here (#189)
Closing for anotherPR |
No description provided.