BillGilbert

Untitled

Feb 1st, 2019
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.04 KB | None | 0 0
  1. import java.util.Arrays;
  2. import java.util.LinkedList;
  3.  
  4. public class Delete2 {
  5.     private static final int LIMIT_ELEMENT = 2;
  6.  
  7.     public static void main(String args[]) {
  8.  
  9.         int[] inputArray = {1, 1, 3, 4, 4, 4, 4, 4, 5, 6, 7, 7};
  10.  
  11.         Arrays.sort(inputArray);
  12.  
  13.         System.out.println("Source array");
  14.         Arrays.stream(inputArray).forEach(System.out::println);
  15.  
  16.         LinkedList<Integer> newArray = new LinkedList<>();
  17.  
  18.         int countOfElement = 1;
  19.         int currentElement = inputArray[1];
  20.         newArray.add(currentElement);
  21.  
  22.         for (int i=1; i<inputArray.length; i++) {
  23.             if (inputArray[i] != currentElement ) {
  24.                 countOfElement = 1;
  25.                 currentElement = inputArray[i];
  26.             } else {
  27.                 ++countOfElement;
  28.             }
  29.  
  30.             if (countOfElement<=LIMIT_ELEMENT) {
  31.                 newArray.add(currentElement);
  32.             }
  33.         }
  34.  
  35.         System.out.println("Result");
  36.         newArray.forEach(System.out::println);
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment