BillGilbert icon

Untitled

BillGilbert | PRO | 02/01/19 11:15:06 PM UTC | 0 ⭐ | 276 👁️ | Never ⏰ | []
Java |

1.04 KB

|

None

|

0 👍

/

0 👎

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);
    }
}

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎