Skip to content

Instantly share code, notes, and snippets.

@deedee47
Last active April 12, 2021 12:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save deedee47/04801070c8889d863d0b7d393db9e1dd to your computer and use it in GitHub Desktop.
Save deedee47/04801070c8889d863d0b7d393db9e1dd to your computer and use it in GitHub Desktop.
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;
}
@deedee47
Copy link
Author

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