Skip to content

LinkedList Lab #13

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

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
Open

LinkedList Lab #13

wants to merge 11 commits into from

Conversation

jkpasaoa
Copy link

No description provided.

};

size() {
let count = 0;

Choose a reason for hiding this comment

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

Can you think of a way to have this function run in O(1) instead of O(n)?

Comment on lines +117 to +127
let currentNode = this.head;
let dataSet = new Set();
while (currentNode) {
if (dataSet.has(currentNode.data)) {
return true;
} else {
dataSet.add(currentNode.data)
};
currentNode = currentNode.next;
};
return false;

Choose a reason for hiding this comment

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

Using the toArray method and comparing the set's length to that of the array

Suggested change
let currentNode = this.head;
let dataSet = new Set();
while (currentNode) {
if (dataSet.has(currentNode.data)) {
return true;
} else {
dataSet.add(currentNode.data)
};
currentNode = currentNode.next;
};
return false;
let arr = this.toArray;
let dataSet = new Set(arr);
return !(array.length === dataSet.size);

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.

2 participants