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.
Be the first one to answer
Add answer anonymously...
Intellect Design Arena Java Developer Interview Questions
Stay ahead in your career. Get AmbitionBox app
qr-code
Helping over 1 Crore job seekers every month in choosing their right fit company
65 L+

Reviews

4 L+

Interviews

4 Cr+

Salaries

1 Cr+

Users/Month

Contribute to help millions

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

Follow us
  • Youtube
  • Instagram
  • LinkedIn
  • Facebook
  • Twitter