-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Create: 2306 - Naming a Company (ts,js,py) #2692
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
javascript/2306-naming-a-company.js
Outdated
|
||
var distinctNames = function (ideas) { | ||
const wordMap = new Map(); | ||
let res = 0; |
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.
Suggestion: provide a better name
count or distinctCount
javascript/2306-naming-a-company.js
Outdated
ideas.forEach((word) => { | ||
const ch = word.charAt(0); | ||
const substr = word.slice(1); | ||
if (!wordMap.has(ch)) { |
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.
Suggestion: leverage a one liner
const set = (map.get(char) ?? new Set());
set.add(char);
map.set(char, set);
javascript/2306-naming-a-company.js
Outdated
wordMap.get(ch).add(substr); | ||
}); | ||
|
||
for (const [ch1, set1] of wordMap.entries()) { |
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.
Suggestion: you can also iterate on key values without using map api
for (const [ ch1, set1 ] of wordMap) { ... }
javascript/2306-naming-a-company.js
Outdated
const wordMap = new Map(); | ||
let res = 0; | ||
|
||
ideas.forEach((word) => { |
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.
Suggestion: avoid functional for each of you can
javascript/2306-naming-a-company.js
Outdated
if (ch1 === ch2) { | ||
continue; | ||
} | ||
let intersect = 0; |
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.
Suggestion: move to helper function
const intersect = getIntersect();
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.
Thanks! Updated the code
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.
Few other suggestions pending
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.
Updated code: now using for loop instead of forEach.
javascript/2306-naming-a-company.js
Outdated
|
||
function getIntersection(set1, set2) { | ||
let intersect = 0; | ||
for (const word of set1) { |
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 suggested avoid functional looping, but in this case you can take advantage of filter when doing set intersections
for (const word of set1) { | |
const getIntersection = (set1, set2) => [ ...set1 ] | |
.filter((key) => set2.has(key)) | |
.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.
here as well updated the code.
Important
Please make sure the file name is lowercase and a duplicate file does not already exist before merging.