Recorded because I had a hard time in the introduction to paiza's skill check.
Scanner sc = new Scanner(System.in);
List<String> words = new ArrayList<>();
while(sc.hasNext()) {
String s = sc.nextLine();
if (words.contains (s)) {// Check if it's already in the list and delete it if it exists words.remove(s); } words.add (0, s); // Add the entered string to the beginning of the list }
for(String word : words) {
System.out.println(word);
}
-If you increase or decrease the value of ArrayList with remove () or add (), all the stored values must be shifted, so it takes time to process. -Since contains () of ArrayList also searches all the values in the list, the more the stored values, the longer the processing time.
-Use HashSet to search for character strings. → Elements can be converted to hash values and used for searching, and values can be found quickly.
Add all the strings entered to the list and use HashSet to hide duplicates. (I don't actually delete it, I assume it wasn't there)
Scanner sc = new Scanner(System.in);
List<String> words = new ArrayList<>();
while(sc.hasNext()) {
String s = sc.nextLine();
words.add(s);
}
Collections.reverse (words); // Invert the list to output from the newly entered one
Set
for(String word : words) {
if (! usedWords.contains (word)) {// Determine if it has already been added to usedWord (duplicate) and display if not System.out.println(word); } usedWords.add(word); }
Recommended Posts