-
Notifications
You must be signed in to change notification settings - Fork 2
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 #5
base: master
Are you sure you want to change the base?
Pr 5 #5
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.
We can add a return if isAllUnique = false instead to make the program run more efficiently
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.
You may want to consider exiting loop if isAllUnique==false. Otherwise, it will loop through the whole array.
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.
If you add a return as soon as you find a duplicate, it will be more efficient because it won't have to keep searching through the rest of the array
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.
Consider limiting the number of loops within loops to improve time efficiency
@@ -0,0 +1,16 @@ | |||
|
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.
Add a comment to describe what type of uniqueness you are looking for (unique characters? unique array items?)
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.
I feel like we should be returning isAllUnique right away if it is false so that the code doesn't have to continue through the entire array before returning.
@@ -0,0 +1,14 @@ | |||
|
|||
const isAllUniqueElements = (array) => { |
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.
Could we rename to areAllElementsUnique? Might read a little more smoothly ...
}); | ||
|
||
return isAllUnique; | ||
}; |
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 easy-to-read function
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.
Returns true for empty array -- should it return null instead?
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.
Can we refactor to exit the loop as soon as it is false?
const isAllUniqueElements = (array) => { | ||
let isAllUnique = true; | ||
|
||
array.forEach((currentElement, currentIndex) => { |
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.
good function to check for unique elements
This method determines if each element is unique.