Skip to content

Instantly share code, notes, and snippets.

@Amaka202
Last active April 12, 2021 09:42
Show Gist options
  • Save Amaka202/05f16a5e3fd17f1b9cde423954ed32d3 to your computer and use it in GitHub Desktop.
Save Amaka202/05f16a5e3fd17f1b9cde423954ed32d3 to your computer and use it in GitHub Desktop.
Algorithm Fridays Week 1
// Given a sorted array nums, write a function to remove the
// duplicates from the array such that each element appears
// only once. Your function should return the new length
const removeDuplicateArrayElements = (nums) => {
const noDuplicatesArray = [];
for(let i = 0; i < nums.length; i++){
//compare adjacent elements of the array and check if they
//are the same since the array is sorted already, skip if true.
if(nums[i] === nums[i + 1]) continue;
noDuplicatesArray.push(nums[i]);
}
return noDuplicatesArray.length;
}
console.log(removeDuplicateArrayElements([0,0,1,1,2,2,3,3,4,4,4,5,5,5,6])
); //7
@Amaka202
Copy link
Author

Nice Amaka. This is actually very good. However I feel your space complexity of O(n) can be reduced to O(1). What if rather than pushing into a new array, you define a counter and increment when nums[i] != nums[i+1]? Let me know what your think.

Yes I didn't think of that😫😫. Thank you so much, since the deadline has elapsed I will apply the feedback to subsequent problems. I am already excited about this program Algorithm Fridays as I hope to improve on my problem solving skills.💃🏻

@Amaka202
Copy link
Author

Lol I like your style. Good

Thank you so much🙏🏻

@meekg33k
Copy link

Hello @Amaka202, thank you for participating in Week 1 of Algorithm Fridays.

I really like the logic you implemented by comparing adjacent elements in the array, awesome stuff! While your solution works and passes most of the test cases, it fails on one.

The assumption your solution makes is that the input array cannot have a null value because if I pass a null value to your function, it would break on line 8. Ideally, you always want to write code that is robust and takes care of edge cases and unexpected input. Basically adding a if (!nums) return 0 check would suffice.

Also, is there a way we could have solved this problem without having to create another array like you did on line 6? Because if you think about it, all you care about is the number of unique elements and doing that would reduce your space usage from O(N) to O(1). What do you think?

I have posted my solution here, do let me know what you think.

@Amaka202
Copy link
Author

Hello @Amaka202, thank you for participating in Week 1 of Algorithm Fridays.

I really like the logic you implemented by comparing adjacent elements in the array, awesome stuff! While your solution works and passes most of the test cases, it fails on one.

The assumption your solution makes is that the input array cannot have a null value because if I pass a null value to your function, it would break on line 8. Ideally, you always want to write code that is robust and takes care of edge cases and unexpected input. Basically adding a if (!nums) return 0 check would suffice.

Also, is there a way we could have solved this problem without having to create another array like you did on line 6? Because if you think about it, all you care about is the number of unique elements and doing that would reduce your space usage from O(N) to O(1). What do you think?

I have posted my solution here, do let me know what you think.

Yup Thanks alot Uduak for the feedback, I could have definitely improved the code by not creating an extra array and using a counter instead. And also for the cases I overlooked, I have learned today to account for every possible case in my code. Thanks!

I will take out time to update the code for future reference.

Looking forward to this week's problem!💃🏻💃🏻

@enaiho
Copy link

enaiho commented Apr 12, 2021

Nice Amaka. This is actually very good. However I feel your space complexity of O(n) can be reduced to O(1). What if rather than pushing into a new array, you define a counter and increment when nums[i] != nums[i+1]? Let me know what your think.

Yes I didn't think of that😫😫. Thank you so much, since the deadline has elapsed I will apply the feedback to subsequent problems. I am already excited about this program Algorithm Fridays as I hope to improve on my problem solving skills.💃🏻

Great stuff Amaka and Weldone!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment