Hackerrank Cut the sticks Solution
Improving the algorithm

Realize that you don't actually need to remove elements from a collection. You can just count how many sticks would disappear in each step.

Take for example this sorted list of sticks:


1 1 1 2 2 3 3 3 3 3 4 5 5 5



At each cutting step, the shortest sticks disappear:


1 1 1 2 2 3 3 3 3 3 4 5 5 5
^^^^^
3 will disappear

      2 2 3 3 3 3 3 4 5 5 5
      ^^^
      2 will disappear

          3 3 3 3 3 4 5 5 5
          ^^^^^^^^^
          5 will disappear



And so on. You can just count the number of sticks that will disappear, and figure out from that how many will remain.

Use interface types in declarations

Instead of this:


ArrayList&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(size);



It's recommended to declare variables with their interface types:

List&lt;Integer&gt; list = new ArrayList&lt;Integer&gt;(size);


Style

This is way too compact writing style:


for(int i=0;i&lt;size;i++)



It's much more readable this way, by putting spaces around operators:

for (int i = 0; i &lt; size; i++)


Checking an empty list

Instead of this:


    while(list.size()&gt;0)



A more idiomatic way to write:

    while(!list.isEmpty())


Suggested implementation

Putting the above tips together:

public static void main(String[] args) {
    List&lt;Integer&gt; sticks = readSticksFromStdin();
    Collections.sort(sticks);

    int pos = 0;
    int remaining = sticks.size();
    while (0 &lt; remaining) {
        System.out.println(remaining);
        int count = countEqualFrom(sticks, pos);
        pos += count;
        remaining -= count;
    }
}

private static int countEqualFrom(List&lt;Integer&gt; sticks, int from) {
    int value = sticks.get(from);
    for (int i = 1; from + i &lt; sticks.size(); ++i) {
        if (value != sticks.get(from + i)) {
            return i;
        }
    }
    return sticks.size() - from;
}

private static List&lt;Integer&gt; readSticksFromStdin() {
    Scanner scanner = new Scanner(System.in);
    int num = scanner.nextInt();
    List&lt;Integer&gt; sticks = new ArrayList&lt;&gt;(num);
    for (int i = 0; i &lt; num; ++i) {
        sticks.add(scanner.nextInt());
    }
    return sticks;
}