I found two way to find the duplicate value from string array.
First way :
private static String FindDupValue(String[] sValueTemp) {
for (int i = 0; i < sValueTemp.length; i++) {
String sValueToCheck = sValueTemp[i];
if(sValueToCheck==null || sValueToCheck.equals(""))continue;
for (int j = 0; j < sValueTemp.length; j++) {
if(i==j)continue;
String sValueToCompare = sValueTemp[j];
if (sValueToCheck.equals(sValueToCompare)){
return sValueToCompare;
}
}
}
return "";
}
Second way :
private static String FindDupValueUsingSet(String[] sValueTemp) {
Set<String> sValueSet = new HashSet<String>();
for(String tempValueSet : sValueTemp) {
if (sValueSet.contains(tempValueSet))
return tempValueSet;
else
if(!tempValueSet.equals(""))
sValueSet.add(tempValueSet);
}
return "";
}
Both methods are correct.
My question is which one best method and why? Or is there any other best way to find out duplicate value form an array?