Skip to content

Instantly share code, notes, and snippets.

@deedee47
Last active April 27, 2021 10:24
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/88585a284c8191e6af9960a758601dd3 to your computer and use it in GitHub Desktop.
Save deedee47/88585a284c8191e6af9960a758601dd3 to your computer and use it in GitHub Desktop.
Returns new count of elements in a Numbers array excluding occurrences of a specified value
public int countOfElementsExcludingVal(int[] nums, int val){
if(nums == null) return 0;
int countWithoutVal = nums.length;
for(int item : nums){
if (item == val) countWithoutVal--;
}
return countWithoutVal;
}
@meekg33k
Copy link

Hello @deedee47, thank you for participating in Week 2 of Algorithm Fridays.

This is a really decent solution; I like that you included edge case checks and you interpreted the problem differently to come up with a memory-optimal solution. Really neat!

Your solution focuses on looping through the array to check elements with values not equal to val. Do you think there's a faster way we could loop through the array?

I've posted my solution here. Do let me know what you think.

@deedee47
Copy link
Author

Hi @meekg33k, thank you for your comment.

I have gone through your solution, really insightful with the double-pointer approach. Thank you

@meekg33k
Copy link

No worries, glad you found it insightful!

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