Squash repeats
easy
New here? How this page works
- Read the Problem tab. Read it twice. Look at the examples: what goes in, what should come out.
- Open Break it down. Answer the questions in your own words. This is your plan.
- Write code on the right. Start small. Stuck on a blank page? Press “Write my plan as comments”.
- Press Run examples. See which examples pass. Red ones show what your code gave and what was expected.
- Stuck? Use Hints, one at a time. Still stuck after all of them? Read the Walkthrough. That's learning, not cheating.
- Press Submit. It runs hidden tests too. Pass them all, and it's solved.
nums is sorted from smallest to largest, and may contain repeats, like [1, 1, 2, 3, 3, 3].
Move one copy of each different value to the front of the list, in order, by overwriting items in nums itself. Then return the front part that holds them: for the example, [1, 2, 3].
Use only a few extra variables, not a second list or a Set. (Slicing the front off at the very end is fine.)
Examples
Example 1
- Input
- nums = [1, 1, 2, 3, 3, 3]
- Output
- [1, 2, 3]
Example 2
- Input
- nums = [-2, 0, 0, 0, 5]
- Output
- [-2, 0, 5]
Example 3
- Input
- nums = [4, 4, 4]
- Output
- [4]
Every item is the same, so only one is kept.
Rules
numsis sorted from smallest to largest and has between 0 and 100,000 numbers.
Words on this page, in plain English (3)Open if any word is new to you
- Variable
- A labelled box that holds a value. You give it a name, put something in it, and use the name later to get it back.
- Array (list)
- A list of values kept in order, inside square brackets. Like a row of numbered lockers.
- Sorted
- In order, smallest to biggest (or A to Z). Many fast tricks only work on sorted lists.
Answer each question in your own words before you look at ours. Rough notes are fine: the point is to think before you type code. Your answers stay in this browser.
1.What goes in?
Name every input and what it looks like. Can it be empty? Sorted? Negative?
Stuck? Each hint gives a little more away. Try the problem again after each one.
This explains the whole solution.
You will learn the most by trying first, even if you don’t finish. But if this pattern is new to you, reading the walkthrough first is a fine way to learn it.
1. Understand the problem
The list is sorted, so all copies of a value sit next to each other: [1, 1, 2, 3, 3, 3]. We keep one of each, in order, by moving them to the front of the same list, then return that front part: [1, 2, 3].
2. Solve an example by hand
Read the list left to right and keep a value only if it's different from the last one you kept. 1: keep. 1: same as last kept, skip. 2: keep. 3: keep. 3, 3: skip. Kept: 1, 2, 3.
Because the list is sorted, comparing with just the last kept value is enough. You never need to look further back.
3. The simple way
return [...new Set(nums)]; gives the right answer in one line. A Set keeps the first copy of each value, in order. It's a perfectly good answer in real code! But it builds a Set and a new list, O(n) extra memory, and this problem asks us to work in place, a skill you'll need when memory is tight.
4. Spot the pattern
We need to keep some items and drop others, in place. That's the read and write pointers template: read visits every item, write marks where the next kept item goes. Both move the same way, write never ahead of read.
5. The two-pointer idea
Read every item, write only new values
nums
function squashRepeats(nums) { let write = 0; // where the next new value goes for (let read = 0; read < nums.length; read++) { if (write === 0 || nums[read] !== nums[write - 1]) { nums[write] = nums[read]; write++; } } return nums.slice(0, write);}write marks where the next new value goes. Nothing is kept yet.
Why is it safe to overwrite nums[write]? Because write is never ahead of read. Anything at write or before has already been read, so overwriting it can't lose a value we still need.
6. The code, line by line
let write = 0; // where the next new value goeswritecounts how many values we've kept, which is also where the next one goes.for (let read = 0; read < nums.length; read++) {readvisits every item, start to end.if (write === 0 || nums[read] !== nums[write - 1]) { nums[write] = nums[read]; write++; }Keep
nums[read]if it's the first item (write === 0) or differs from the last kept value,nums[write - 1]. Keeping means copying it to positionwrite, then movingwriteon.return nums.slice(0, write);The first
writeitems are the answer.slice(0, write)returns them as a list.
7. How fast is it?
| Set | Read and write pointers | |
|---|---|---|
| Time | O(n) | O(n): one pass |
| Extra memory | O(n) | O(1), apart from the final slice |
Common mistakes
- Comparing with the next item (
nums[read + 1]). It works, but going off the end of the list is easy. Comparing with the last kept value is simpler. - Removing items with
splicein a loop. Eachspliceshifts everything after it, which makes the whole thing O(n²), and changing a list while looping over it causes skipped items.
Memorise the solution, once you understand it.
This shows the full solution, then helps you rebuild it from memory in four steps. Remembering it is what lets you write it again in an interview, or in a week. Your result goes into your reviews, so it comes back just before you'd forget.
Learn it by heart: Squash repeats
Read the 10 lines slowly. For each one, say out loud what it does and why it is there. Then cover it and move on to step 2.
function squashRepeats(nums) { let write = 0; // where the next new value goes for (let read = 0; read < nums.length; read++) { if (write === 0 || nums[read] !== nums[write - 1]) { nums[write] = nums[read]; write++; } } return nums.slice(0, write);}