Removing duplicates from a sorted array of literals in JavaScript


The problem statement says that given a sorted array of numbers as an input source by the user such that we need to remove the duplicates or repeated elements from it making it all new array containing only unique elements inside it.

What is a Sorted Array in JavaScript ?

A sorted array is a kind of array in javascript that follows a certain order of line in which each element is sorted in alphabetical and numerical order all before using the sort method in javascript itself. The already sorted speed array lets you speed up the search and perform the other operations on the sorted array more easily and efficiently.

The output of the problem statement is to remove the repeated and redundant elements present in the number array can be visualized like :

const sortedArray = [ 1 , 2 , 2 , 3 , 4 , 6 ,6 ];

// perform algorithm to remove duplicates 

const duplicateElementRemovedArray = [ 1 , 2 , 3 , 4 , 6 ];

Algorithm

Step 1 : Declare a function with name removeDuplicates that takes a sorted array as an input source as mentioned in the problem statement .

Step 2 : Traverse each and every element of the array from 0th index to the length of the array using outer for loop .

Step 3 : With inner loop , we start traversing from backwards from the length of the sorted array i.e. last element till the one index increment of outer loop i value .

Step 4 : The comparison operator is now used to check and compare one value of i in outer loop with every value of j loop at each iteration to check if any duplicates exist or not .

Step 5 : If found any duplicates , splice method is used to delete one particular element that is a duplicate value at i or j index once equality match gets successful , to discard and delete out duplicate element , where the second argument in splice method tells how many characters to delete about.

Step 6 : Once all the checking and removing of duplicates are done using a nested loop , the unique elements sorted array is successfully returned.

Example

function removeDuplicates(sortedArray)
{
   for(let i = 0 ; i < sortedArray.length ; i++)
   {
     for( j = sortedArray.length ; j>=i+1 ; j--)
     {
       if(sortedArray[i] === sortedArray[j])
       {
         sortedArray.splice(i,1);
       }
     }
   }
   return sortedArray;
}

const sortedArray = [ 1,1,2,3,4,4,5,6,6,7,8];
const uniqueElementsArray = removeDuplicates(sortedArray);
console.log(uniqueElementsArray);

Output

[
  1, 2, 3, 4,
  5, 6, 7, 8
]

Time and Space Complexity

In the above algorithm we have initially used nested loops that takes array.length traversal hence resulting O(n^2) time complexity and splice method of JavaScript also takes O(n) time complexity to extract or delete out certain element hence resulting O(n^2) + O(n) = O(n^2) worst case time complexity .

But the space complexity is O(1) i.e. constant as we are not allocating any extra memory to complete the task of removing duplicates from sorted array of numbers . Later we can also call the function using and the output in console would be :

Conclusion

This is how we can solve the above problem statement thinking logically and in the context of coding going from nested looping for traversal and access of array elements and splice javascript method to complete the execution of problem statement.

Updated on: 22-Aug-2023

180 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements