In this BlueJ Program all the duplicate values from an array will be eliminated. Actually we cannot delete any location from an array, we will do some manipulation.
Related Post: BlueJ Programs on Numeric Array
import java.util.*;
class Max
{
Scanner sc=new Scanner(System.in);
int a[],b[];
int n,i,j,x;
public void take()
{
System.out.print("\nHow many Elements to
Store: ");
n=sc.nextInt();
a=new int[n];
b=new int [n];
for(i=0;i< n;i++)
{
System.out.print("\nEnter Value: ");
a[i]=sc.nextInt();
}
System.out.println("\nArray Elements As
Follows.");
for(i=0;i< n;i++)
{
System.out.print(" "+a[i]);
}
}
public void elimination()
{
x=0;
for(i=0;i< n;i++)
{
for(j=0;j< x;j++)
{
if(a[i]==b[j])
break;
}
if(j==x)
b[x++]=a[i];
}
System.out.println("\nArray Elements After Deletion of Duplicate
Value");
for(i=0;i< x;i++)
{
System.out.print(" "+b[i]);
}
}
public static void main(String args[])
{
Max ob=new Max();
ob.take();
ob.elimination();
}
}
Another Technique
import java.util.*;
class Max
{
Scanner sc=new Scanner(System.in);
int a[],b[];
int n,i,j,x;
public void take()
{
System.out.print("\nHow many Elements to Store: ");
n=sc.nextInt();
a=new int[n];
b=new int [n];
for(i=0;i< n;i++)
{
System.out.print("\nEnter Value: ");
a[i]=sc.nextInt();
}
System.out.println("\nArray Elements As Follows.");
for(i=0;i< n;i++)
{
System.out.print(" "+a[i]);
}
}
public void elimination()
{
int y;
for(i=0;i<n-1;i++)
{
for(j=i+1;j<n;j++)
{
if(a[i]>a[j])
{
x=a[i];
a[i]=a[j];
a[j]=x;
}
}
}
x=0;
y=a[0];
b[x++]=y;
for(i=1;i< n;i++)
{
if(a[i]!=y)
{
b[x++]=a[i];
y=a[i];
}
}
System.out.println("\nArray Elements After Deletion of Duplicate Value");
for(i=0;i< x;i++)
{
System.out.print(" "+b[i]);
}
}
public static void main(String args[])
{
Max ob=new Max();
ob.take();
ob.elimination();
}
}
Sample Input Output
How many Elements to Store: 4
Enter Value: 2
Enter Value: 3
Enter Value: 3
Enter Value: 12
Array Elements As Follows.
2 3 3
12
Array Elements After Deletion of Duplicate
Value
2 3 12
Related Post: BlueJ Programs on Numeric Array
No comments:
Post a Comment