Essential storage keeps sign-in and learning features working. Google Analytics measures visits; Microsoft Clarity records interactions to help us improve usability. Both are optional and stay off until you choose. Read our privacy policy.
Deduplicate a sorted integer array coding exercise | SovranCode
Remove duplicates in-place from an ascending integer array and return the number of unique values. The retained prefix must preserve ascending order.
Requirements
Use no second array
Return 0 for a null pointer or zero length
Only write within the original array
Example
Input
{1, 1, 2, 2, 4}, 5
Expected output
length 3: {1, 2, 4}
Show a hint
Write down the input contract and the failure cases before coding.
tests
Runs the open file in the same safe C subset used in the C course
RUNTIME OUTPUT
Ready to run the file open above against 1 test.
LEARN FROM THE SOLUTION
Why the solution works.
Because the input is sorted, a value is new precisely when it differs from the last retained value. The read index visits every original item while write marks the next free position in the valid prefix, giving linear time and constant extra space.
What this exercise teaches
Read/write indices
In-place algorithms
Array contracts
A PRACTICAL PLAN
Work through Deduplicate a sorted integer array with intent.
Translate the contract.Turn the requirements into a short checklist before editing your-solution.c.
Use the example as evidence.Predict the result for the supplied input, then add one boundary case such as an empty value, a limit, or unexpected input.
Review the implementation.Compare your choices against solution.c only after a real attempt.
EXERCISE FAQ
Before you move on.
What does “Deduplicate a sorted integer array” teach?
This intermediate C exercise focuses on Read/write indices, In-place algorithms, Array contracts. Its requirements define the exact behavior to implement before you write code.
How should I validate this C solution?
Start with the displayed example input and expected output, then test a boundary case suggested by the requirements. Open your-solution.c and select Run code. The browser uses the same safe C subset as the C course, including a test harness for exercise functions.
When should I open the reference solution?
Attempt Deduplicate a sorted integer array first. Then open the read-only solution file to compare the contract, edge-case handling, and implementation choices—not simply to copy the final code.