Skip to content

Instantly share code, notes, and snippets.

@anil477
Last active February 4, 2021 19:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anil477/c2349420b7ebca121ef82ca30b771bcd to your computer and use it in GitHub Desktop.
Save anil477/c2349420b7ebca121ef82ca30b771bcd to your computer and use it in GitHub Desktop.
Remove Duplicate in sorted array
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);
}
}
@kushalsethi
Copy link

Does the time complexity for this is o(n^2)?

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