Day 3: Coding Challenge
Shraddha M Thakur
Software Developer | Actively Seeking Opportunities | Computer Applications Graduate | Passionate about Product Innovation
Problem - Given an integer array nums sorted in increasing order, remove the duplicates in place such that each unique element appears only once. The relative order of the elements should be kept the same. Then return the number of unique elements in nums.
Consider the number of unique elements of nums to be k, to get accepted, you need to do the following things:
Change the array nums such that the first k elements of nums contain the unique elements in the order they were present in nums initially. The remaining elements of nums are not important as well as the size of nums.
Return k.
Solutions -
Brute force solution - We will use the HashSet data structure to insert the unique elements in the array. Because Set has a property that does not include Duplicates.
Time Complexity - O(NLogN)
Space Complexity - O(N)
Optimal solution -In this solution we will use the two-pointer approach we will compare the elements and check whether the two elements are similar are not. If it is different then we will assign the jth index value to the ith index value.
Time Complexity - O(N)
Space Complexity - O(1)
#Codingchallenge #problemsolvingskills #problemsolving #codeforgood #60daysofchallenge #60dayscode #consistency #dsaforproblemsolving