Skip to content

Instantly share code, notes, and snippets.

@MichaelDimitras
Last active May 7, 2018 18:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MichaelDimitras/ccd499063353eda2e525832d92f3bfc8 to your computer and use it in GitHub Desktop.
Save MichaelDimitras/ccd499063353eda2e525832d92f3bfc8 to your computer and use it in GitHub Desktop.
Vowel doubler
/*
* A function that takes an array of lowercase characters and returns
* an array in which all of the vowels are repeated.
* Time complexity: O(n)
* Space complexity: O(1)
*/
const vowelDoubler = function(arr) {
let numVowels = 0;
const vowels = new Set(['a', 'e', 'i', 'o', 'u']);
for(let i = 0; i < arr.length; i++) {
if(vowels.has(arr[i])) {
numVowels += 1;
}
}
let idx = arr.length - 1;
arr = arr.concat(new Array(numVowels));
while(idx >= 0) {
arr[idx + numVowels] = arr[idx];
if(vowels.has(arr[idx])) {
numVowels -= 1;
arr[idx + numVowels] = arr[idx];
}
idx -= 1;
}
return arr;
}
console.log(vowelDoubler(['a','d','e']))
console.log(vowelDoubler(['a']))
console.log(vowelDoubler(['d','y', 'r']))
console.log(vowelDoubler([]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment