I need to convert this code containing multiple for loops and if statements to Java 8 Streams:
public static List<String> removeDuplicateNames(String[] namesArray){
List<String> uniqueNames = new ArrayList<>();
int occurrence = 0;
for(String name: namesArray) {
for (String name2 : namesArray)
if (name.equalsIgnoreCase(name2))
occurrence++;
if (occurrence == 1)
uniqueNames.add(name);
occurrence = 0;
}
return uniqueNames;
}
I need to loop through an array of names and return a list containing only the names that are not duplicate. If a name appears more than once I need to remove it from the final List.
Input -> ["Tom", "George", "Tom", "Mike", "Brian"]
Output -> ["George", "Mike", "Brian"]
Can anyone please help out?
Aucun commentaire:
Enregistrer un commentaire