-
Notifications
You must be signed in to change notification settings - Fork 5
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
Pr 5: isAllUniqueElements #4
base: master
Are you sure you want to change the base?
Conversation
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { | ||
if (elementToCompare === currentElement) { | ||
isAllUnique = false; | ||
} | ||
}); | ||
}); |
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.
Is there a more time/space efficient way to do this? without making a new arrays/nested loops?
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.
perhaps a recursive solution?
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 improves space complexity.
array.forEach((currentElement, currentIndex) => { | |
array.slice(currentIndex + 1).forEach((elementToCompare) => { | |
if (elementToCompare === currentElement) { | |
isAllUnique = false; | |
} | |
}); | |
}); | |
array.forEach((currentElement, currentIndex) => { | |
if (array[currentIndex + 1, -1].includes(currentElement)) { | |
isAllUnique = false; | |
} | |
}); | |
}); |
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.
Nice job overall!
let isAllUnique = true; | ||
|
||
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { |
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.
What's the time and space complexity? Can we improve either of them?
array.slice(currentIndex + 1).forEach((elementToCompare) => { | |
if (array[currentIndex + 1, -1].includes(currentElement)) { | |
isAllUnique = false; | |
} |
|
||
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { | ||
if (elementToCompare === currentElement) { |
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.
The '===' operator could cause some issues if the elements are not the exact match (even in type) and this might create some issues, Using '==' operator would help in avoiding these issues.
let isAllUnique = true; | ||
|
||
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { |
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 am not sure of the purpose of both slice and forEach on this line of code.
Please clarify.
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { |
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.
Can we avoid having a nested loop if possible? Maybe we can first populate a hash with the elements and then use that to lookup each element.
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.
Is it possible to return early if we find a duplicate element?
No description provided.