Showing posts with label extract unique elements from array. Show all posts
Showing posts with label extract unique elements from array. Show all posts

Friday 3 January 2014

program to fetch unique elements from Array.

Write a program to fetch unique elements from Array.

package programs;

import java.util.LinkedHashSet;
import java.util.Set;

public class Arrayuniqno {
public static void main(String[] args) {
int[] array1 = { 1, 1, 2, 1, 3, 4, 5, 8, 8, 8, 8 };

System.out.println("Original Array1  Elements1:");
for (Integer x : array1) {
System.out.print(x + " ");
}
Set<Integer> UniqueNumbers = new LinkedHashSet<Integer>();
for (int x : array1) {
UniqueNumbers.add(x);
}
System.out.println("");
System.out.println("Array2 with Unique Elements: ");
Object[] array2 = UniqueNumbers.toArray();
for (Object x : array2) {
System.out.print(x + " ");
}
}
}