Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.LinkedList;
- public class Delete2 {
- private static final int LIMIT_ELEMENT = 2;
- public static void main(String args[]) {
- int[] inputArray = {1, 1, 3, 4, 4, 4, 4, 4, 5, 6, 7, 7};
- Arrays.sort(inputArray);
- System.out.println("Source array");
- Arrays.stream(inputArray).forEach(System.out::println);
- LinkedList<Integer> newArray = new LinkedList<>();
- int countOfElement = 1;
- int currentElement = inputArray[1];
- newArray.add(currentElement);
- for (int i=1; i<inputArray.length; i++) {
- if (inputArray[i] != currentElement ) {
- countOfElement = 1;
- currentElement = inputArray[i];
- } else {
- ++countOfElement;
- }
- if (countOfElement<=LIMIT_ELEMENT) {
- newArray.add(currentElement);
- }
- }
- System.out.println("Result");
- newArray.forEach(System.out::println);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment