settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Unique Usernames - Java Task (Sets)

+13 votes

Write a simple program that reads from the console a sequence of usernames and keeps a collection with only the unique ones.

Print the collection on the console in order of insertion:

Input

Output

6

Ivan

Ivan

Ivan

SemoMastikata

Ivan

Hubaviq1234

Ivan

SemoMastikata

Hubaviq1234

 
asked in Java category by user nikole
edited by user golearnweb

1 Answer

+1 vote
 
Best answer

Here's my solution:

import java.util.LinkedHashSet;
import java.util.Scanner;

public class Pr_01_UniqueUsernames {
    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        LinkedHashSet<String> set = new LinkedHashSet<>();
        String input = sc.nextLine();

        for (int i = 0; i < n; i++) {
            input = sc.nextLine();
            set.add(input);
        }

        for (String s : set) {
            System.out.println(s);
        }
    }
}

 

answered by user samfred5830
selected by user golearnweb
...