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 #5

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

Pr 5 #5

wants to merge 2 commits into from

Conversation

CheezItMan
Copy link

This method determines if each element is unique.

array.forEach((currentElement, currentIndex) => {
array.slice(currentIndex + 1).forEach((elementToCompare) => {
if (elementToCompare === currentElement) {
isAllUnique = false;
Copy link

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;

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;

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

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 @@

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;
Copy link

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

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

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;

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;

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

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

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.

9 participants