AfterAcademy Tech
•
07 Feb 2020

Difficulty: Medium
Asked in: Google, Microsoft, Amazon
Given a sorted array that has some unique as well as some duplicate elements, your task is to remove all the duplicate elements from the given array and return the size of the new array that is not having any duplicate elements in it.
For example:
Input: A={2,3,3,4,5,7,7,9}
Output: 6
Explanation: All the duplicate elements have been removed and new array is A={2,3,4,5,7,9} i.e. length 6.
Similarly,
Input: A={4,7,9,9,10,11,11}
Output: 5
Possible follow-up questions to ask the interviewer: →
You can try solving the question from here.
We are going to discuss two possible ways to solve this problem:
Solution idea
The idea here is create another array of size equal to the original array and to compare an element with other elements just adjacent to it. If there is a match, then there is a duplicate element present in the array. We will only copy the unique elements into auxiliary array and will also maintain a count of all the unique elements. After traversing the whole array we will copy all the elements of the auxiliary array back to the original array and return the original array.
Solution steps
Pseudo-code
int removeDuplicatesFromSortedArray(int A[], int size)
{
int index=0
int tempArr[size]
if(size <= 1) //Accessing i+1 will give garbage in this case
return size
for( i = 0 to size-2 )
{
if(A[i]!=A[i+1])
{
tempArr[index] = A[i]
index += 1
}
}
tempArr[index] = A[size-1] //Store the last element
index++;
for( i = 0 to index-1 )
A[i] = tempArr[i]
return index
}
Complexity Analysis
Time Complexity: O(N), where N is the size of the array.
Space Complexity: O(N)(Why?)
Critical ideas to think!
Solution idea
The idea in this solution is the same as the approach we followed in the above solution, but instead, we will store the unique elements in the same array. How can we do that?(Think!)
We can use the index variable used above to update the original array. The duplicates elements will be replaced by unique neighboring elements.
Solution steps
Pseudo-code
int removeDuplicatesFromSortedArray(int A[], int size)
{
if(size<=1)
return size
int index = 0
for( i = 0 to size-2 )
{
if(A[i] != A[i+1])
{
A[index] = A[i]
index += 1
}
}
A[index] = A[size-1]
index++;
return index
}
Complexity Analysis
Time Complexity: O(N)
Space Complexity: O(1)
Happy Coding! Enjoy Algorithms!!
AfterAcademy Tech
This is an interview problem asked by companies like Myntra, Visa, Oracle, Adobe, etc. The problem is to remove the duplicate nodes from the given Linked List so that only unique nodes(with unique data values) are present.

AfterAcademy Tech
Given an array say A having n elements, the task is to sort the array in wavy manner. In wavy manner the array is in the form where A[0] >= A[1] <= A[2] >= A[3] <= A[4] >= …..

AfterAcademy Tech
Given an array of integers A[ ], if i < j and A[i] > A[j] then the pair (i, j) is called the inversion of an array A[ ]. You need to write a program to find the total counts of inversion in an array A[ ]. The inversion count of an array suggests how close the array is from being sorted. This problem has been asked in Amazon and Google's interview.

AfterAcademy Tech
Given two integer array A[] and B[] of size m and n(n <= m) respectively. We have to check whether B[] is a subset of A[] or not. An array B is a subset of another array A if each element of B is present in A. (There are no repeated elements in both the arrays)
