Efficient code for extracting unique elements from a sorted list of array