Continue learning
Practice related DSA questions in the same topic.
Return the distinct values of the sorted input in their original order.
Return the distinct values of the sorted input in their original order.
def solve(values, target): count = 0 i = 0 while (i < len(values)): if ((i == 0) or (values[i] != values[(i - 1)])): count = (count + 1) i = (i + 1) out = [0] * (count) i = 0 write = 0 while (i < len(values)): if ((i == 0) or (values[i] != values[(i - 1)])): out[write] = values[i] write = (write + 1) i = (i + 1) return outIdentify a run start at position 0.
Identify a run start at position 0.
A class register needs one entry per group of identical name tags.
Count and copy run starts; every different value starts a new run.
O(n) time · O(u) output for u distinct values auxiliary space
Worked example · {"values":[1,1,2,4,4],"target":0} ⇒ [1,2,4]
Edge case 1 · {"values":[],"target":0} ⇒ []
Edge case 2 · {"values":[2,2],"target":0} ⇒ [2]
An empty sequence contains zero distinct values.
How could you compact the original array using a write pointer?