Hey there! This problem might be a bit unpredictable, but give it a go and let us know how you do!
Write a Java program that reads a single string as input, representing a list of names separated by commas (with no spaces). Your program should discard all duplicate names and print all unique names in the same order they appear in the input. Note that names are case-sensitive i.e., 'John' and 'john' are considered two different names. For example, if you input 'John,Tim,John,Casey,Tim,Casey', the output should be 'John,Tim,Casey'.The Set interface
Removing duplicate names from a comma-separated list
Report a typo
Sample Input 1:
John,Tim,John,Casey,Tim,CaseySample Output 1:
John,Tim,CaseySample Input 2:
Robert,Julia,Martin,Julia,RobertSample Output 2:
Robert,Julia,MartinWrite a program in Java 17
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Read the string input from the user
String input = scanner.nextLine();
// Split the string by commas into an array of names
String[] names = input.split(",");
// TODO: Implement a Set to keep track of unique names
// The Set interface is part of Java's collections framework and is used
// to store unique elements - duplicates are automatically disregarded
}
}
___
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.
Create a free account to access the full topic
By continuing, you agree to the JetBrains Academy Terms of Service as well as Hyperskill Terms of Service and Privacy Policy.