Last active
December 31, 2018 11:06
-
-
Save sagarsumit03/2f1f3128f287e08242747837f6fe6aea to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| class Solution { | |
| public int removeDuplicates(int[] nums) { | |
| //if the list is empty return 1. | |
| if(nums.length == 0) return 0; | |
| //Fast Pointer. | |
| int j = 0; | |
| //to maintain the length. | |
| int count = 1; | |
| //Looping through the array: | |
| for(int i = 0; i < nums.length; i++) | |
| { | |
| //If the entries are not same then copy whats in the ith index to jth index and increment j. | |
| //Example: 1,1,2 now as i keeps increasing nums[i] becomes 2 so, we copy 2 to the j++ index means the second 1. | |
| if(nums[i]!=nums[j]) | |
| { | |
| j++; | |
| count++; | |
| nums[j] = nums[i]; | |
| } | |
| } | |
| return count; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment