Practice
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Frontend UI Machine Coding
Resources
Career Advice and Roadmaps
Data Structures and Algorithms
Machine Coding Round (LLD)
System Design & Architecture (HLD)
Backend Development
Frontend Development
Project Ideas for Software Developers
Core Computer Science
Companies
SDE Jobs & Internships
Interview Questions
Compare Companies
IDE
Online IDE
Collaborative IDE

Unique Elements in Sorted Array Editorial

DSA Editorial, Solution and Code

Practice Problem Link: Unique Elements in Sorted Array | Practice Problem

Please make sure to try solving the problem yourself before looking at the editorial.

Problem Statement

Given a sorted array A, find the size of array A after removing the duplicate elements.

Naive Approach

We can just insert all the elements of the array in a set and return the size of the set.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(n)

Implementation

C++
int removeDuplicates(vector<int> &A) {
   unordered_set<int> duplicateRemoved;
   for (int i = 0; i < A.size(); i++) {
       duplicateRemoved.insert(A[i]);
   }
   return (int)duplicateRemoved.size();
}
Java
class Solution {
   int removeDuplicates(int[] A) {
       HashSet<Integer> duplicateRemoved = new HashSet<>();
       for (int i = 0; i < A.length; i++) {
           duplicateRemoved.add(A[i]);
       }
       return (int)duplicateRemoved.size();
   }
}

Optimal Approach

We will use the fact that the array is sorted to our advantage. Consider the count of unique elements initially to be the size of the array.

We keep two pointers at the endpoints of the array and check for duplicates at those indexes.

If duplicates are found, decrement the count of distinct elements accordingly. Finally, return the updated count.

Analysis

  • Time Complexity: O(n)
  • Auxiliary Space Complexity: O(1)

Implementation

C++
int removeDuplicates(vector<int> &A) {
   int count = A.size(), left = 0, right = (int)A.size() - 1;
   while (left < right) {
       while (left != right && A[left] == A[left + 1]) {
           count--;
           left++;
       }
       while (left != right && A[right] == A[right - 1]) {
           count--;
           right--;
       }
       left++;
   }
   return count;
}
Java
class Solution {
   int removeDuplicates(int[] A) {
       int count = A.length, left = 0, right = A.length - 1;
       while (left < right) {
           while (left != right && A[left] == A[left + 1]) {
               count--;
               left++;
           }
           while (left != right && A[right] == A[right - 1]) {
               count--;
               right--;
           }
           left++;
       }
       return count;
   }
}
Related Content
Dutch National Flag
k-diff pairs
K-Subarray Sum
k-Substring Vowels
Kth element of two sorted lists
Maximum K-Subarray Sum
Maximum k-Substring Vowels
Merge Two Sorted Arrays
Remove occurences
Sorted Arrays Intersection
Three Sum
Trapping Rain Water
Two Sum Sorted
SDE Bootcamp - Become a software engineer at a product-based company
Practice Data Structures & Algorithms
Learning Resources
Interview Prep Resources
Community
Join our community
Blog
  • Career Advice and Roadmaps
  • Data Structures & Algorithms
  • Machine Coding Round (LLD)
  • System Design & Architecture
  • Backend Development
  • Frontend Development
  • Awesome Project Ideas
  • Core Computer Science
Practice Questions
  • Machine Coding (LLD) Questions
  • System Design (HLD) Questions
  • Topic-wise DSA Questions
  • Company-wise DSA Questions
  • DSA Sheets (Curated Lists)
  • JavaScript Interview Questions
  • Frontend UI Machine Coding Questions
Online Compilers (IDE)
  • Online Java Compiler
  • Online C++ Compiler
  • Online C Compiler
  • Online Python Compiler
  • Online JavaScript Compiler
Topic-wise Problems
  • Dynamic Programming Interview Questions
  • Linked List Interview Questions
  • Graph Interview Questions
  • Backtracking Interview Questions
  • Arrays Interview Questions
  • Trees Interview Questions
Company-wise Problems
  • Amazon Interview Questions
  • Microsoft Interview Questions
  • Google Interview Questions
  • Flipkart Interview Questions
  • Adobe Interview Questions
  • Facebook Interview Questions
DSA Sheets (Curated Lists)
  • Top Interview Questions
  • FAANG Interview Questions
  • Most Asked Interview Questions
  • 6 month DSA Practice Sheet
  • 3 month DSA Practice Sheet
  • Last minute DSA Practice Sheet