Skip to content

Conversation

AHTHneeuhl
Copy link
Contributor

Important

Please make sure the file name is lowercase and a duplicate file does not already exist before merging.


var distinctNames = function (ideas) {
const wordMap = new Map();
let res = 0;
Copy link
Collaborator

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

ideas.forEach((word) => {
const ch = word.charAt(0);
const substr = word.slice(1);
if (!wordMap.has(ch)) {
Copy link
Collaborator

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);

wordMap.get(ch).add(substr);
});

for (const [ch1, set1] of wordMap.entries()) {
Copy link
Collaborator

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) { ... }

const wordMap = new Map();
let res = 0;

ideas.forEach((word) => {
Copy link
Collaborator

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

if (ch1 === ch2) {
continue;
}
let intersect = 0;
Copy link
Collaborator

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();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Updated the code

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Few other suggestions pending

Copy link
Contributor Author

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.

@AHTHneeuhl AHTHneeuhl requested a review from aakhtar3 July 19, 2023 06:05

function getIntersection(set1, set2) {
let intersect = 0;
for (const word of set1) {
Copy link
Collaborator

@aakhtar3 aakhtar3 Jul 22, 2023

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

Suggested change
for (const word of set1) {
const getIntersection = (set1, set2) => [ ...set1 ]
.filter((key) => set2.has(key))
.length;

Copy link
Contributor Author

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.

@AHTHneeuhl AHTHneeuhl requested a review from aakhtar3 August 6, 2023 11:43
@AHTHneeuhl AHTHneeuhl closed this by deleting the head repository Oct 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants