How to print duplicate string in given strings?

#2
Re: how to print duplicate values in given string

import java.util.*;
class FindDuplicates {
public static void main(String[] args) {
String[] str = {"chait","ram","sri","chait"};
HashSet s = new HashSet();
for (String str1 : str)
if (!s.add(str1)) {
System.out.println("Duplicate Values detected: " + str1);
}
System.out.println(s.size() + " distinct values: " + s);
}
}

Output:
Duplicate values detected: chait
3 distinct values:[chait, sri, ram]

Regards,

Ramsri4ever.

 
Top