Compact a Sorted Array with possible duplicates