β Remove Duplicates Inplace(Contest)
Remove Duplicates Inplace easy Time Limit: 2 sec Memory Limit: 128000 kB
```java
import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework
// don't change the name of this class
// you can add inner classes if needed
class Main {
public static int removeK(int[] arr,int n)
{
int ans=0;
int i=0;
while(i<n)
{
int j=i+1;
while(j<n&& arr[i]==arr[j])
{
j++;
}
if((j-i)>2)
{
ans+=((j-i)-2);
}
i=j;
}
return n-ans;
}
public static void main (String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int arr[] =new int[n];
for(int i=0;i<n;i++) {
arr[i] =sc.nextInt();
}
System.out.println(removeK(arr,n));
}
}
```Last updated