Created
May 16, 2023 11:34
-
-
Save ritik-agrawal/680facd8262ebfa6fa8658402823cb34 to your computer and use it in GitHub Desktop.
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
class Solution { | |
public int removeDuplicates(int[] ar) { | |
var nui = 1; | |
var cnt = 1; | |
var len = ar.length; | |
for(int cur = 1; cur < len; cur++){ | |
if(ar[cur-1] == ar[cur]){ | |
cnt++; | |
} else { | |
cnt = 1; | |
} | |
if (nui < cur){ | |
ar[nui] = ar[cur]; | |
} | |
if (cnt <= 2){ | |
nui++; | |
} | |
} | |
return nui; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Leet Code
Remove duplicates from the given array with at most twice the repetition.
Achievement
The above question is solved by me. The solution provided is in place which means that the space complexity is O(n) and it beats 100% of the total submissions with a runtime of 0ms as computed by LeetCode.
Link of the submission: click here