Remove Duplicates from a Sorted Array

Given a sorted integer array ARR of size N, you need to remove duplicates such that each element appears only once and return the length of this new array.

Input:

The first line of input contains an integer ‘T’ denoting the number of test cases. The first line of each test case contains an integer ‘N’ denoting the number of elements in the array. The second line of each test case contains ‘N’ space-separated integers representing the elements of the array.

Output:

For each test case, return the length of the modified array.

Example:

Input:
ARR = [1, 1, 2]
Output:
2
Explanation:

Because ARR is modified to [1, 2] with length 2.

Constraints:

  • 1 <= T <= 5
  • 1 <= N <= 10^5
  • -10^9 <= ARR[i] <= 10^9
  • Do not allocate extra space for another array. Modify the given input array in-place with O(1) extra memory.
  • Time limit: 1 sec

Note:

You don't need to print anything, it has already been taken care of. Just implement the given function.
AnswerBot
4mo

Remove duplicates from a sorted array in-place and return the length of the modified array.

  • Use two pointers approach to track unique elements and duplicates.

  • Modify the input array in-place without usi...read more

Help your peers!
Select
Add answer anonymously...

Intellect Design Arena Java Developer interview questions & answers

A Java Developer was asked 6mo agoQ. Write an SQL query to find the 3rd highest salary of an employee.
A Java Developer was asked 8mo agoQ. What are the differences between HashSet and Hashtable?
A Java Developer was asked Q. How can you optimize the performance of an application?

Popular interview questions of Java Developer

A Java Developer was asked 6mo agoQ1. Write an SQL query to find the 3rd highest salary of an employee.
A Java Developer was asked 8mo agoQ2. What are the differences between HashSet and Hashtable?
A Java Developer was asked Q3. How can you optimize the performance of an application?
Intellect Design Arena Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
play-icon
play-icon
qr-code
Trusted by over 1.5 Crore job seekers to find their right fit company
80 L+

Reviews

10L+

Interviews

4 Cr+

Salaries

1.5 Cr+

Users

Contribute to help millions

Made with ❤️ in India. Trademarks belong to their respective owners. All rights reserved © 2025 Info Edge (India) Ltd.

Follow Us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter
Profile Image
Hello, Guest
AmbitionBox Employee Choice Awards 2025
Winners announced!
awards-icon
Contribute to help millions!
Write a review
Write a review
Share interview
Share interview
Contribute salary
Contribute salary
Add office photos
Add office photos
Add office benefits
Add office benefits