Skip to content
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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

Pr 5: isAllUniqueElements #4

wants to merge 2 commits into from

Conversation

CheezItMan
Copy link

No description provided.

Comment on lines +5 to +11
array.forEach((currentElement, currentIndex) => {
array.slice(currentIndex + 1).forEach((elementToCompare) => {
if (elementToCompare === currentElement) {
isAllUnique = false;
}
});
});
Copy link

@TaylorMililani TaylorMililani Mar 11, 2021

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?

Choose a reason for hiding this comment

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

perhaps a recursive solution?

Copy link

@Jing-321 Jing-321 Mar 12, 2021

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.

Suggested change
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;
}
});
});

Copy link

@Jing-321 Jing-321 left a 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) => {
Copy link

@Jing-321 Jing-321 Mar 12, 2021

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?

Suggested change
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) {
Copy link

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) => {

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.

Comment on lines +5 to +6
array.forEach((currentElement, currentIndex) => {
array.slice(currentIndex + 1).forEach((elementToCompare) => {

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.

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?

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.

7 participants