Compact the Array

Easy
array two-pointers
Problem Description

Remove consecutive duplicates from an array, keeping only one occurrence of each consecutive group.

Input Format
First line contains n (array size). Second line contains n integers separated by spaces.
Output Format
Array with consecutive duplicates removed, elements separated by spaces.
Constraints
1 <= n <= 1000
1 <= array elements <= 100
Sample Input/Output
Input:
8
1 1 2 2 2 3 1 1
Output:
1 2 3 1
Explanation

Remove consecutive duplicates: [1,1] → [1], [2,2,2] → [2], [3] → [3], [1,1] → [1]. Result: [1,2,3,1].

Solution Hints
  • Read carefully: Understand input/output format and constraints
  • Start simple: Think of brute force solution first, then optimize
  • Edge cases: Consider empty inputs, single elements, max constraints
  • Data structures: Choose arrays, hash maps, sets based on problem needs
  • Time complexity: Ensure your solution fits within time limits
  • Test examples: Verify logic with provided sample inputs
90
Points
2000ms
Time Limit
256MB
Memory Limit
Code Editor
Register to Submit
Register to Access Code Editor

Create a free account to solve coding problems and track your progress.

Register Now
Feedback