C Program
#include<stdio.h> int main() { int a[100], n, j=0; scanf("%d", &n); for(int i=0; i<n; i++) scanf("%d", &a[i]); for(int i=0; i<n; i++) if(i==0 || a[i]!=a[i-1]) a[j++] = a[i]; for(int i=0; i<j; i++) printf("%d ", a[i]); }
C Output
Input: 7 1 1 2 2 3 4 4 Output: 1 2 3 4
C++ Program
#include<iostream> using namespace std; int main() { int a[100], n, j=0; cin >> n; for(int i=0; i<n; i++) cin >> a[i]; for(int i=0; i<n; i++) if(i==0 || a[i]!=a[i-1]) a[j++] = a[i]; for(int i=0; i<j; i++) cout << a[i] << " "; }
C++ Output
Input: 6 2 2 3 3 3 5 Output: 2 3 5
JAVA Program
import java.util.*; class RemoveDup { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n = sc.nextInt(), a[] = new int[n], j=0; for(int i=0; i<n; i++) a[i] = sc.nextInt(); for(int i=0; i<n; i++) if(i==0 || a[i]!=a[i-1]) a[j++] = a[i]; for(int i=0; i<j; i++) System.out.print(a[i] + " "); } }
JAVA Output
Input: 5 1 2 2 3 3 Output: 1 2 3
Python Program
a = list(map(int, input().split())) print(*[a[i] for i in range(len(a)) if i==0 or a[i]!=a[i-1]])
Python Output
Input: 1 1 1 2 3 3 Output: 1 2 3
In-Depth Learning – Full Concept in Paragraphs
What Is "Remove Duplicates from Sorted Array"?
While programming, whenever you're working with sorted data, you may often get duplicate values. The problem here is to remove that list in-place, which means you overwrite the original array and don't use additional space, so every value comes only once and keeps its sorted order.
How the Code Works
We traverse the array, checking the current element against the last. If it's not the same, it's a new (different) number. We assign this to the front of the array with a new index j, keeping track of where the next distinct value can be placed.
j = 0: where to write the next distinct value.
If a[i] != a[i-1], it's distinct → put in a[j], then increment j.
Lastly, print a[0] through a[j-1].
This maintains the original array unchanged up to the jth index, now containing the distinct values.
Python utilizes list comprehension to filter and display only elements not repeated.
Example
Input:
[1, 1, 2, 3, 3, 3, 4]
Process:
Compare every element with the one before it.
Add only when they are different.
Result: [1, 2, 3, 4]
Output:
1 2 3 4
Real-Life Analogy
Let's say you have a list of registered guests for an event, but due to error, people signed up multiple times. The list being sorted alphabetically, you can simply go through it and delete duplicate names and retain the first occurrence of each.
It occurs in databases at the time of:
Eliminating duplicate records
Normalizing data
Merging sorted records
Where and When Is It Used?
This logic is extensively applied in:
Data cleanup (deduplication)
Optimization of search results
Data compression
Log analysis
Database indexing
It is also a standard interview coding question and array-processing algorithm, particularly in corporations such as Google, Amazon, Infosys, and TCS.
Time and Space Complexity
Operation\tComplexity
Time\tO(n)
Space\tO(1) (in-place, no additional memory)
Python list comprehension version utilizes a new list → technically O(n) space.
Python-Specific Tip
For longer arrays or performance, use:
python
from itertools import groupby
print(*[k for k, _ in groupby(a)])
Or if not limited by "in-place":
python
print(*sorted(set(a)))
SEO-Optimized Natural Paragraph for Ranking
Want to delete duplicates from a sorted list in C, C++, Java, or Python? This tutorial demonstrates how to delete duplicate values from a sorted list in-place with basic and efficient logic. Deleting duplicates simplifies data, saves memory, and makes lookups faster. You're working with logs, search results, or interview questions, this is an important concept. Learn how to eliminate duplicates with least space and quickest logic in all programming languages with real-world use cases and step-by-step examples.
Social Plugin