-
Notifications
You must be signed in to change notification settings - Fork 0
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
|
||
const isAllUniqueElements = (array) => { | ||
let isAllUnique = true; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let's check for if the array is empty and return appropriately. |
||
|
||
array.forEach((currentElement, currentIndex) => { | ||
array.slice(currentIndex + 1).forEach((elementToCompare) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems a little complicated - consider removing the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This syntax is a little confusing -- does it make more sense to compare currentElement to array[currentIndex + 1] instead of using array.slice(...).forEach(...)? |
||
if (elementToCompare === currentElement) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure why we need to slice here. Can we remove it? I think the nested forEach will take care of comparing each element to the outer forEach element. |
||
isAllUnique = false; | ||
} | ||
}); | ||
}); | ||
|
||
return isAllUnique; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will return true when the array is empty There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good job writing this algorithm. This would run but there might be way to find whether all elements in an array is unique a little more efficient by switching the isAllUnique to false when you find a duplicated value and end the iteration there instead of iterated through all elements of the array |
||
}; | ||
|
||
export default isAllUniqueElements; |
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 may want to assign is AllUnique to something else. If the array is empty, it will evaluate to true.