Example Input :
an array of String contains :String[] str = new String[] {"alok", "dhoni", "manish", "rajiv", "alok", "rajiv"}
output :
unique items: [manish, dhoni]
Duplicate items : [alok, rajiv]
Duplicate items : [alok, rajiv]
SetExampl.java
package com.alok.Uniqueduplicate;
import
java.util.HashSet;
import java.util.Set;
public class SetExample
{
public static void main(String[]
args)
{
String[] str = new String[] {"alok", "dhoni", "manish", "rajiv", "alok", "rajiv"};
Set<String> duplicateSet = new
HashSet<String>();
Set<String> uniqueSet = new
HashSet<String>();
for(int i = 0; i < str.length ; i++ )
{
if(!uniqueSet.add(str[i]))
{
duplicateSet.add(str[i]);
}
}
uniqueSet.removeAll(duplicateSet);
System.out.println("unique
items: "+uniqueSet);
System.out.println("Duplicate
items : "+ duplicateSet);
}
}
output :
unique items: [manish, dhoni]
Duplicate items : [alok, rajiv]
Duplicate items : [alok, rajiv]
No comments:
Post a Comment