In: Computer Science
in java
A NavigableSet is a set that stores it’s element in
order. Say we have a bunch of items
and we want to store them in order but we don’t want any
duplicates. Write a generic
class that implements a structure that does that.
Here is the java program to store elements in order and elements will be printed without duplicates.
import java.util.Scanner;
public class AOrder{
public static void main(String args[])
{
int n,temp;
Scanner s = new
Scanner(System.in);
System.out.print("Enter no.of
elements you want in array:");
n = s.nextInt();
int a[] = new int[n];
System.out.println("Enter all the
elements:");
for (int i=0; i<n; i++)//Loop to
store bunch of elements
{
a[i] =
s.nextInt();
}
for (int i=0; i<n; i++)
{
for (int j=i +
1; j<n; j++)
{
if (a[i] > a[j])//to store elements in
ascending order
{
temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}
}
for (int i=0; i<n; i++)
{
if(i==0)//to
print first element
System.out.print(a[i] + " ");
if(i>0&&a[i]!=a[i-1])//to check duplicates
System.out.print(a[i] + " ");
}
}
}