Created
July 19, 2020 06:05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*find if the array has duplicate by using brute force*/ | |
public boolean bruteforce(String[] arr) { | |
for (int i = 0; i < arr.length; i++) { | |
for (int j = 0; j < arr.length; j++) { | |
if (arr[i].equals(arr[j]) && i != j) | |
return true; | |
} | |
} | |
return false; | |
} | |
/*It will return true if the array has duplicates*/ | |
public boolean hasDuplicates(String[] input){ | |
List inputList = Arrays.asList(input); | |
Set inputSet = new HashSet(inputList); | |
if(inputSet.size()< inputList.size()) | |
return true; | |
return false; | |
} | |
/*This method will return the suplicate value(if present), | |
else a blank string.*/ | |
public String checkDuplicateUsingAdd(String[] input) { | |
Set inputSet = new HashSet(); | |
for (String str : input) { | |
if (!inputSet.add(str)) | |
return str; | |
} | |
return ""; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment