Skip to content

Instantly share code, notes, and snippets.

@Ezeji
Last active April 12, 2021 20:33
Show Gist options
  • Save Ezeji/16edfbb5f5a01b2a9d3d2d98cebab6bd to your computer and use it in GitHub Desktop.
Save Ezeji/16edfbb5f5a01b2a9d3d2d98cebab6bd to your computer and use it in GitHub Desktop.
This algorithm focuses on removing array duplicates.
public static int RemoveArrayDuplicate(int[] nums)
{
int arrayLength = 0;
for (int i = 0; i < nums.Length; i++)
{
if (nums[i] == nums[i++])
{
arrayLength = arrayLength + 1;
}
}
return arrayLength;
}
@meekg33k
Copy link

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

This is the most memory-optimal solution because you aren't using any data structure, awesome stuff!

The only one gotcha I see here is that your solution assumes is that the input array cannot have a null value because what happens if I run RemoveArrayDuplicate(null)? Ideally, you always want to write code that is robust and doesn't fail on edge cases or unexpected input.

That said, your solution got a special mention in this blog post here where I posted our solution. Please read through and let me know what you think.

Thanks once again and see you on Friday!

@Ezeji
Copy link
Author

Ezeji commented Apr 12, 2021

Hi @meekg33k

Thank you really for your efforts and this initiative. It is really a great vision that would have massive impact on the tech ecosystem by providing a learning opportunity for persons like myself who are life-long learners. Well done to you and your team!

I also want to thank you for the special mention on your blog as well as your feedback for improvement through this medium, it means a lot to me.

My question as regards handling null parameter passed to the function for a robust solution is, can such scenario also be taken care of by using a try-catch code construct, in other words, writing a function that performs its intended operation and handle every other out of bound edge cases using exception handling ?

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