Find first non-repeating character in a String

preview_player
Показать описание
--------------------------------------------------------------------------------------------------------------------------------
// Write a Java program that finds the first non-repeating character in a string.
// Ex: abracadabra → c
--------------------------------------------------------------------------------------------------------------------------------
public class Task {

public static void main(String[] args) {
}

public static char findFirstUnique(String str) {

boolean unique = true;
if (i == j) {
continue;
}

unique = false;
break;
}
}

if (unique) {
}
}

return '-';
}
}
Рекомендации по теме
Комментарии
Автор

Thank you for your explaination. I think this solution is much simpler:

public static char firstNonRepeatedChar(String str) {
for(int i = 0; i < str.length() - 1; i++) {
if &&
!(str.substring(0, i).contains(str.charAt(i) + ""))) {
return str.charAt(i);
}
}
throw new RuntimeException("No non repeated char was found.");
}

musamehdiyevv