import java.util.*;
class Main {
public static void main(String[] args) {
int[] arr1 = new int[] {4,0,4,2};
List<Integer> ans = swipeLeft(arr1);
System.out.println(ans);
}
public static List<Integer> swipeLeft(int[] row) {
int left = 0;
while(left < row.length - 1) { //[4,0,4,2]
int right = left+1;
if(row[left] == 0) {
left++;
continue;
}
while(right < row.length) {
if(row[right] == 0) {
right++;
continue;
}
if(row[left] != row[right]) {
left = right;
break;
}
if(row[left] == row[right]) {
row[left] = row[left] + row[right];
row[right] = 0;
left = right;
break;
}
}
}
List<Integer> result = new ArrayList<>();
int count = 0;
for(int i= 0; i < row.length;i++) {
if(row[i] != 0) {
result.add(row[i]);
count++;
}
}
int remainder = row.length - count;
while(remainder-- > 0) {
result.add(0);
}
return result;
}
}