Advertisement
mstoyanova

Remove Duplicates

Feb 25th, 2021
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.08 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class RemoveDuplicates {
  4.     public static void main(String[] args) {
  5.  
  6.         // Write a program that removes all duplicates from a list of elements.
  7.         //
  8.         //1,2,2,2,4,7 -> 1,2,4,7.
  9.         //Maintain the relative order of the remaining items.
  10.  
  11.         Scanner scanner = new Scanner(System.in);
  12.         String line = scanner.nextLine();
  13.  
  14.  
  15.         System.out.println(line);
  16.  
  17.  
  18.         // creating a an Array which eelements we want to compare
  19.  
  20.         String arryLine[] = line.split(",");
  21.  
  22.  
  23.         // looping through each element and comparing it with the  next one
  24.  
  25.  
  26.         for (int i = 0; i < arryLine.length; i++) {
  27.  
  28.                 if (!arryLine[i].equals(arryLine[i + 1])) {
  29.                     System.out.print(arryLine[i] + ",");
  30.                 }
  31.                 else if (arryLine[i].equals(arryLine[i + 1])) {
  32.                 System.out.print("");
  33.                    
  34.                 }
  35.                
  36.                 } System.out.print(arryLine[arryLine.length-1]);
  37.  
  38.  
  39.                 }
  40.             }
  41.  
  42.  
  43.  
  44.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement