Bird
0
0
DSA Cprogramming~30 mins

Remove Duplicates from Sorted Array Two Pointer in DSA C - Build from Scratch

Choose your learning style9 modes available
Remove Duplicates from Sorted Array Two Pointer
📖 Scenario: You have a sorted list of numbers that may contain duplicates. You want to clean it up so that each number appears only once, keeping the order intact.
🎯 Goal: Build a program that removes duplicates from a sorted array using the two-pointer technique and prints the cleaned array.
📋 What You'll Learn
Create an integer array called nums with the exact values: 1, 1, 2, 2, 3, 4, 4, 5
Create an integer variable called length and set it to the size of nums
Use two integer pointers i and j to remove duplicates in-place
Print the array after removing duplicates, showing only unique elements separated by spaces
💡 Why This Matters
🌍 Real World
Removing duplicates from sorted data is common in cleaning up lists like contact numbers, product IDs, or timestamps.
💼 Career
Understanding two-pointer techniques and in-place array modification is useful for coding interviews and efficient data processing.
Progress0 / 4 steps
1
Create the sorted array
Create an integer array called nums with these exact values: 1, 1, 2, 2, 3, 4, 4, 5.
DSA C
Hint

Use int nums[] = { ... }; to create the array with the exact numbers.

2
Set the length of the array
Create an integer variable called length and set it to the number of elements in nums using sizeof(nums) / sizeof(nums[0]).
DSA C
Hint

Use sizeof(nums) / sizeof(nums[0]) to get the number of elements.

3
Remove duplicates using two pointers
Use two integer variables i and j. Initialize i to 0. Use a for loop with j from 1 to length - 1. Inside the loop, if nums[j] is not equal to nums[i], increment i and set nums[i] = nums[j]. After the loop, update length to i + 1.
DSA C
Hint

Use i to track the position of unique elements and j to scan through the array.

4
Print the array after removing duplicates
Use a for loop with variable k from 0 to length - 1. Inside the loop, print nums[k] followed by a space. After the loop, print a newline.
DSA C
Hint

Use printf("%d ", nums[k]); inside the loop and printf("\n"); after the loop.