Remove Duplicate Elements From Array
public class RemoveDuplicte
{
public static void main(String[] args)
{
int[] a = {10,20,20,30,40,60,60,60,90,100}; // Predefined Array
int i,l,j,c;
l = a.length-1; // Subtracting 1 to avoid ArrayIndexOutOfBound Exception as index starts from 0
for(i=0;i<l-1;i++)
{
for(j=i+1;j<l;j++)
{
if(a[i]==a[j]) // Swapping the duplicate element from the last element
{
c = a[j];
a[j] = a[l];
a[l] = c;
l--; // Reducing the length of the array as the last element is duplicate
}
}
}
for(i=0;i<l;i++)
{
System.out.print(a[i] + " ");
}
}
}
{
public static void main(String[] args)
{
int[] a = {10,20,20,30,40,60,60,60,90,100}; // Predefined Array
int i,l,j,c;
l = a.length-1; // Subtracting 1 to avoid ArrayIndexOutOfBound Exception as index starts from 0
for(i=0;i<l-1;i++)
{
for(j=i+1;j<l;j++)
{
if(a[i]==a[j]) // Swapping the duplicate element from the last element
{
c = a[j];
a[j] = a[l];
a[l] = c;
l--; // Reducing the length of the array as the last element is duplicate
}
}
}
for(i=0;i<l;i++)
{
System.out.print(a[i] + " ");
}
}
}
/*
OUTPUT :
10 20 100 30 40 60 90
*/
Comments
Post a Comment