Removing duplicate elements form an array using collections:
- package com.instanceofjavaTutorial;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- public class RemoveDupArray {
- public static void main(String[] args) {
- // A string array with duplicate values
- String[] data = { "E", "C", "B", "E", "A", "B", "E", "D", "B", "A" };
- System.out.println("Original array : " + Arrays.toString(data));
- List<String> list = Arrays.asList(data);
- Set<String> set = new HashSet<String>(list);
- System.out.print("After removing duplicates: ");
- String[] resarray= new String[set.size()];
- set.toArray(resarray);
- for (String ele: resarray) {
- System.out.print(ele + ", ");
- }
- }
- }
- OutPut:
- Original array : [E, C, B, E, A, B, E, D, B, A]
- After removing duplicates: D, E, A, B, C
You Might Like:
1. Java Interview Programs On Core Java
2. Java Interview Programs On Collections
3. What is The Output Of Following Programs Java Interview:
4. Java Concept and Example Program
No comments