Last active
May 7, 2018 18:27
-
-
Save MichaelDimitras/ccd499063353eda2e525832d92f3bfc8 to your computer and use it in GitHub Desktop.
Vowel doubler
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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