Last active
April 27, 2021 10:24
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
Hi @meekg33k, thank you for your comment.
I have gone through your solution, really insightful with the double-pointer approach. Thank you
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
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.