Last active
February 4, 2021 19:46
-
-
Save anil477/c2349420b7ebca121ef82ca30b771bcd to your computer and use it in GitHub Desktop.
Remove Duplicate in sorted array
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
import java.io.*; | |
import java.util.*; | |
class myCode | |
{ | |
public static int[] removeDuplicates(int[] A) { | |
if (A.length < 2) | |
return A; | |
int j = 0; | |
int i = 1; | |
while (i < A.length) { | |
if (A[i] == A[j]) { | |
i++; | |
} else { | |
j++; | |
A[j] = A[i]; | |
i++; | |
} | |
} | |
int[] B = Arrays.copyOf(A, j + 1); | |
for( int i: B) | |
{ | |
System.out.println(" " + i); | |
} | |
return B; | |
} | |
public static void main(String[] args) { | |
// we consider the array is already sorted. | |
int[] arr = { 1, 1, 1, 2, 2, 2, 2, 3, 4, 4, 5, 5 }; | |
arr = removeDuplicates(arr); | |
System.out.println(" length of unique arary " + arr.length); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does the time complexity for this is o(n^2)?