Collections:

SET INTRODUCTION

//Set- no duplicates
Sample Input and Output:
[All text in bold corresponds to the input and rest corresponds to output]


Enter the username
John
Do you want to continue? (Y/N)
Y
Enter the username
Christoper
Do you want to continue? (Y/N)
Y
Enter the username
Ahamed
Do you want to continue? (Y/N)
Y
Enter the username
Ahamed
Do you want to continue? (Y/N)
N
The unique number of usernames is 3

CODE:

import java.util.HashSet;

import java.util.Scanner;

public class Main {

    public static void main(String[] args){
  
        String user;
    Scanner sc = new Scanner(System.in);
HashSet<String> set = new HashSet<String>();
do {
System.out.println("Enter the username");
set.add(sc.nextLine());
System.out.println("Do you want to continue? (Y/N)");
user=sc.nextLine();
}while(user.equalsIgnoreCase("y"));
System.out.println("The unique number of usernames is "+set.size());
}

}

 

Comments

Popular posts from this blog