Delete elements in first string which are not in second string in JavaScript


The problem statement asks the user that given two string arrays as an input by the user , we need to delete the elements present in the first string taking in condition which are not present in the second string in javascript.

The same problem statement can also be viewed as given two arrays of strings , the solution of the problem statement should return back the newer version of original first string array such that the first string array should only contain the elements that are present in second string array , remembering to preserve the order of the original first string while finding the intersection of two string arrays or deleting the elements that are not present in the second string array.

What is a Map in JavaScript ?

The problem statement efficiently uses map data structure as the core logic in removing the elements that are uncommon to the elements present in string array2 in context of main manipulation that is going to happen in string array 1 where both string array1 and string array2 will be given as an input source by the user.

Map is a collection of key−value pair data structure as well as the function in javascript but both work syntactically different. The key and value pair in map data structure can be of any data type. It can be used to insert , delete , update or perform any manipulation operations.

The syntax and the example looks like :

let colorMap = new Map([
        ["Red", 2],
        ["Blue", 3],
        ["Yellow", 5]
    ]);
     
console.log(colorMap);

Output

Map { 'Red' => 2, 'Blue' => 3, 'Yellow' => 5 }

Algorithm

Step 1 − Declare a function with the name findCommonInStringArray that takes string array1 and array2 as an input

Step 2 − Split every character of both string arrays using split method giving in a separator of space as a parameter inside it.

Step 3 − Create a map data structure in javascript that is an efficient data structure to search , insert or delete the elements of the array with best time complexity.

Step 4 − Traverse every character of string array2 elements and insert the count of numeric 1 to every individual and unique element passed.

Step 5 − We have pointed out every individual element of string array 2 using map data structure and flagged numeric value 1 to each character .

Step 6 − Filter the string array1 using filter method in javascript taking in the task of filtering out the elements that are present in the string 2 array as well which is right now mapped in your newMap data structure .

Step 7 − Join the common characters that are present in string array2 in string array1 itself using the join method in javascript that joins the character in the whole string in string array 1

Example

function findCommonInStringArray(arr1,arr2)
{
       let stringArray1 = arr1.split('');
       let stringArray2 = arr2.split('');
    
       const newMap = {};
    
       stringArray2.forEach(element=>{
        newMap[element] =1;
    })
        
    stringArray1 = stringArray1.filter(element => {
      return newMap.hasOwnProperty(element);
   });

   return stringArray1.join('');
}
const str1 = 'abcdefgh';
const str2 = 'bananana';

const newStringArray1 = findCommonInStringArray(str1,str2);
console.log(newStringArray1);

Output

ab

Time and Space Complexity

The time complexity of split method depends on the number of characters you are splitting leading up to O(n) worst case time complexity followed by map data structure which will take O(n) traversal to flag out individual elements with numeric value 1 and filter method in Javascript will also traverse O(n) worst time complexity to filter out the elements not present in string array 2 summing up to O(n) time complexity and solace complexity of O(1) because we are not allocating any extra memory.

Conclusion

This is how we can solve the above problem statement thinking logically and in the context of coding taking help of javascript methods like split and filter method with map data structure in its most efficient use case .

Updated on: 21-Aug-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements