MCPcopy Create free account
hub / github.com/betomoedano/JavaScript-Coding-Interview-Questions / remove

Function remove

strings/one-edit.js:67–103  ·  view source on GitHub ↗
(string1, string2)

Source from the content-addressed store, hash-verified

65}
66
67function remove(string1, string2) {
68 let largestString = string1.length > string2.length ? string1 : string2;
69 let smallestString = string1.length > string2.length ? string2 : string1;
70 let stringAfterRemoving = "";
71
72 const freq = {};
73 let letterToRemove = null;
74
75 for (const char of largestString) {
76 if (!freq.hasOwnProperty(char)) {
77 freq[char] = 1;
78 } else {
79 freq[char]++;
80 }
81 }
82
83 for (const char of smallestString) {
84 if (freq.hasOwnProperty(char)) {
85 freq[char]--;
86 if (freq[char] === 0) {
87 delete freq[char];
88 }
89 }
90 }
91
92 Object.keys(freq).forEach((key) => (letterToRemove = key));
93
94 let removeOne = 1;
95 for (let i = 0; i < largestString.length; i++) {
96 if (largestString[i] === letterToRemove && removeOne > 0) {
97 removeOne--;
98 continue;
99 }
100 stringAfterRemoving += largestString[i];
101 }
102 return [smallestString, stringAfterRemoving];
103}

Callers 1

oneEditFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected