Solution: Array-Based (JavaScript)
function removeChar(str, char) {
const result = [];
for (let i = 0; i < str.length; i++) {
if (str[i] !== char) {
result.push(str[i]);
}
}
return result.join('');
}
// Tests
console.log(removeChar("banana", 'a')); // "bnn"
console.log(removeChar("hello", 'l')); // "heo"
console.log(removeChar("aaa", 'a')); // ""
console.log(removeChar("abc", 'd')); // "abc"
Built-In Methods
function removeCharBuiltIn(str, char) {
// JavaScript replace with regex
return str.replace(new RegExp(char, 'g'), '');
}
// Or using split/join
function removeCharSplitJoin(str, char) {
return str.split(char).join('');
}
console.log(removeCharBuiltIn("banana", 'a')); // "bnn"
console.log(removeCharSplitJoin("banana", 'a')); // "bnn"
Remove Multiple Characters
function removeChars(str, charsToRemove) {
const removeSet = new Set(charsToRemove);
const result = [];
for (let i = 0; i < str.length; i++) {
if (!removeSet.has(str[i])) {
result.push(str[i]);
}
}
return result.join('');
}
console.log(removeChars("banana", "an")); // "b"
console.log(removeChars("hello world", "lo")); // "he wrd"
In-Place (For Char Arrays)
function removeCharInPlace(chars, char) {
// chars is an array of characters (mutable)
let writeIndex = 0;
for (let readIndex = 0; readIndex < chars.length; readIndex++) {
if (chars[readIndex] !== char) {
chars[writeIndex] = chars[readIndex];
writeIndex++;
}
}
// Truncate array or mark end
chars.length = writeIndex;
return chars;
}
// Test
const chars = ['b', 'a', 'n', 'a', 'n', 'a'];
removeCharInPlace(chars, 'a');
console.log(chars); // ['b', 'n', 'n']
Step-by-Step: In-Place Example
Remove 'a' from "banana":
Initial: ['b', 'a', 'n', 'a', 'n', 'a']
read=0, write=0
read=0: 'b' !== 'a', write 'b' at 0, write++
['b', 'a', 'n', 'a', 'n', 'a']
read=1, write=1
read=1: 'a' === 'a', skip
read=2, write=1
read=2: 'n' !== 'a', write 'n' at 1, write++
['b', 'n', 'n', 'a', 'n', 'a']
read=3, write=2
read=3: 'a' === 'a', skip
read=4, write=2
read=4: 'n' !== 'a', write 'n' at 2, write++
['b', 'n', 'n', 'a', 'n', 'a']
read=5, write=3
read=5: 'a' === 'a', skip
read=6, write=3
Result: ['b', 'n', 'n'] (length = 3)
Case-Insensitive Removal
function removeCharCaseInsensitive(str, char) {
const lowerChar = char.toLowerCase();
const result = [];
for (let i = 0; i < str.length; i++) {
if (str[i].toLowerCase() !== lowerChar) {
result.push(str[i]);
}
}
return result.join('');
}
console.log(removeCharCaseInsensitive("BaNaNa", 'a')); // "BNN"
Complexity Analysis
| Approach |
Time |
Space |
| String concatenation |
O(n²) |
O(n) |
| Array-based |
O(n) |
O(n) |
| In-place |
O(n) |
O(1) |
| Built-in replace |
O(n) |
O(n) |
Common Mistakes
- String concatenation in loop: Inefficient in languages with immutable strings
- Modifying string while iterating: Can cause issues or skip characters
- Not handling empty string: Should return empty string
- Case sensitivity: Clarify if 'A' and 'a' are different
Follow-Up Questions
Q: Remove substring instead of character?
A: Use sliding window or KMP algorithm, more complex
Q: Remove all whitespace?
A: Check for space, tab, newline - use set of whitespace chars
Q: Keep only certain characters?
A: Invert the logic - keep instead of remove
Related Problems