How do you detect and delete if there more than two duplicates in an array. In the following example, I have taken an array with the elements "10, 20, 30, 40, 40, 40, 50, 60, 70, 80 ,10". Here 40 is repeated thrice and 10 is repeated twice. I could write a program to detect two duplicates but I am not able to shrink 40(repeated thrice) to once. Note:- I want to do this without using any java collection
public class ArrayDuplicate {
public void run1()
{
int[] a = {10, 20, 30, 40, 40, 40, 50, 60, 70, 80 ,10};
int size=a.length;
System.out.println("Array size before duplicate deletion "+size);
for(int i =0;i<(size-1);i++)
{
for(int j=i+1;j<=(size-1);j++)
{
if(a[i]==a[j] &&i!=j)
{
while(j<(size-1))
{
a[j]=a[j+1];
j++;
}
size--;
}
}
}
System.out.print("The array after deleting the duplicates is ");
for(int k=0;k<=(size-1);k++)
{
System.out.print(a[k]); //40 is being printed twice
if(k<(size-1))
{
System.out.print(",");
}
else
System.out.print(".");
}
}
public static void main(String[] args)
{
ArrayDuplicate ob = new ArrayDuplicate();
ob.run1();
}
}