Remove Duplicates from Sorted Array Using Two Pointers
📖 Scenario: You have a sorted list of numbers representing daily temperatures recorded over several days. Some temperatures are repeated because the sensor recorded the same temperature multiple times in a row. You want to clean this data by removing duplicate temperatures so that each temperature appears only once, keeping the order intact.
🎯 Goal: Build a program that removes duplicates from a sorted list of numbers using the two-pointer technique. The program should modify the list in place and print the cleaned list without duplicates.
📋 What You'll Learn
Create a list called
temperatures with the exact values: [30, 30, 32, 32, 32, 35, 36, 36, 38]Create a variable called
write_index and set it to 1Use a
for loop with variable read_index starting from 1 to iterate over temperaturesInside the loop, compare
temperatures[read_index] with temperatures[write_index - 1]If they are different, assign
temperatures[read_index] to temperatures[write_index] and increment write_index by 1After the loop, slice
temperatures to keep only elements up to write_indexPrint the final
temperatures list💡 Why This Matters
🌍 Real World
Cleaning sensor data or logs often requires removing repeated values to simplify analysis.
💼 Career
Understanding two-pointer techniques is useful for optimizing array and list operations in software development.
Progress0 / 4 steps