Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Returns the Count of Distinct Elements from an Sorted Array of Numbers,
public int uniqueCountOfElements(int[] nums)
{
if (nums == null) return 0;
int arrSize = nums.length;
if (arrSize == 0) return 0;
int finalCount = 1; // Count the first element by default
for(int count = 0; count < arrSize; count++)
{
if (count != arrSize - 1 && nums[count] != nums[count+1]) finalCount++;
}
return finalCount;
}
@meekg33k

This comment has been minimized.

Copy link

@meekg33k meekg33k commented Apr 12, 2021

Hello @deedee47, thank you for participating in Week 1 of Algorithm Fridays and congratulations, you are the winner of the $20 award 🎉🎉.

Your solution was selected because it is most memory-optimal and it is robust - takes care of edge cases, especially the edge case where the input array has a null value. You were also able to interpret the problem as returning the number of unique elements in an array thus avoiding unnecessary work in your code.

I have made a blog post here about the different solutions and about you as our winner.

We will contact you in less than 24 hours for your award.

Congratulations once again!

@deedee47

This comment has been minimized.

Copy link
Owner Author

@deedee47 deedee47 commented Apr 12, 2021

Thank You @meekg33k for setting this up. Very well appreciated.

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