Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class RemoveDuplicates {
- public static void main(String[] args) {
- // Write a program that removes all duplicates from a list of elements.
- //
- //1,2,2,2,4,7 -> 1,2,4,7.
- //Maintain the relative order of the remaining items.
- Scanner scanner = new Scanner(System.in);
- String line = scanner.nextLine();
- System.out.println(line);
- // creating a an Array which eelements we want to compare
- String arryLine[] = line.split(",");
- // looping through each element and comparing it with the next one
- for (int i = 0; i < arryLine.length; i++) {
- if (!arryLine[i].equals(arryLine[i + 1])) {
- System.out.print(arryLine[i] + ",");
- }
- else if (arryLine[i].equals(arryLine[i + 1])) {
- System.out.print("");
- }
- } System.out.print(arryLine[arryLine.length-1]);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement