|
| 1 | +//return first non-repeating char - O notation = O(2n) so O(n) |
| 2 | +const firstNonRepeat = (string) => { |
| 3 | + let charCount = {}; |
| 4 | + |
| 5 | + if (!string) { |
| 6 | + return "No string input."; |
| 7 | + } |
| 8 | + |
| 9 | + for (let i = 0; i < string.length; i++) { |
| 10 | + let char = string[i]; |
| 11 | + if (charCount[char]) { |
| 12 | + charCount[char]++; |
| 13 | + } else { |
| 14 | + charCount[char] = 1; |
| 15 | + } |
| 16 | + } |
| 17 | + |
| 18 | + for (let j = 0; j < string.length; j++) { |
| 19 | + let charChecking = string[j]; |
| 20 | + if (charCount[charChecking] == 1) { |
| 21 | + return string.charAt(j); |
| 22 | + } |
| 23 | + } |
| 24 | + |
| 25 | + return "No non-repeating characters." |
| 26 | +} |
| 27 | + |
| 28 | +console.log(`Returns first non-repeating character: ${firstNonRepeat("ararta")}`); |
| 29 | + |
| 30 | +/***********************************/ |
| 31 | +//removes all characters in a specified string from another string |
| 32 | +//O(n*m) - likely O(n) because m<n |
| 33 | +const removeChars = (string, remove) => { |
| 34 | + let newString = ""; |
| 35 | + |
| 36 | + for (let i = 0; i < string.length; i++) { |
| 37 | + let deleteMe = false; |
| 38 | + |
| 39 | + for (let j = 0; j < remove.length; j++) { |
| 40 | + if (remove.charAt(j).toLowerCase() == string.charAt(i).toLowerCase()) { |
| 41 | + deleteMe = true; |
| 42 | + } |
| 43 | + } |
| 44 | + if (!deleteMe) { |
| 45 | + newString = newString.concat(string[i]); |
| 46 | + }; |
| 47 | + } |
| 48 | + |
| 49 | + return newString; |
| 50 | +} |
| 51 | + |
| 52 | +console.log(`Removes all characters in a specified string from another string: ${removeChars("yaywowdogewow", "wow")}`); |
| 53 | + |
| 54 | +//more efficient way O(n) This uses a hash table! |
| 55 | +const otherRemoveChars = (input, remove) => { |
| 56 | + let r = {}; |
| 57 | + let newString = ''; |
| 58 | + |
| 59 | + //r[c] is creating a property c (which is the characters of remove) that holds a boolean. |
| 60 | + remove.split('').forEach(c => r[c] = true); |
| 61 | + //now when you go through input you'll be able to check quickly if r has a property of that character that has a boolean. If it does, then you don't copy it. |
| 62 | + input.split('').forEach((c) => { |
| 63 | + if (typeof r[c] === 'undefined') { |
| 64 | + newString += c; |
| 65 | + } |
| 66 | + }); |
| 67 | + |
| 68 | + return newString; |
| 69 | +} |
| 70 | + |
| 71 | +console.log(`Removes all characters in a specified string from another string (more efficient): ${otherRemoveChars("yaywowdogewow", "wow")}`); |
| 72 | + |
| 73 | + |
| 74 | +//chris' soooooper efficient way |
| 75 | +const otherRemoveChars2 = (input, remove) => { |
| 76 | + let r = {}; |
| 77 | + |
| 78 | + remove.split('').forEach(c => r[c] = true); |
| 79 | + return input.split('') |
| 80 | + .filter(c => typeof r[c] === 'undefined') |
| 81 | + .reduce((t, c) => { |
| 82 | + return t += c |
| 83 | + }, ''); |
| 84 | +} |
| 85 | + |
| 86 | +console.log(`Removes all characters in a specified string from another string (Chris' way): ${otherRemoveChars2("yaywowdogewow", "wow")}`); |
| 87 | + |
| 88 | +/***********************************/ |
| 89 | +//reverse the words! |
| 90 | +const reverseEasy = (string) => { |
| 91 | + |
| 92 | + let stringArr = string.split(" "); |
| 93 | + |
| 94 | + let reverseStr = stringArr.reverse().join(" "); |
| 95 | + return reverseStr; |
| 96 | +} |
| 97 | + |
| 98 | +console.log(`Reverse dem words (easy way): ${reverseEasy("i am ameiiizing")}`) |
| 99 | + |
| 100 | +const reverseHard = (string) => { |
| 101 | + let reverseArr = []; |
| 102 | + let wordLength = 0; |
| 103 | + |
| 104 | + for (let i = string.length; i >= 0; --i) { |
| 105 | + if (string[i] == " ") { |
| 106 | + reverseArr.push(string.substr(i + 1, wordLength)); |
| 107 | + wordLength = 0; |
| 108 | + } else if (i == 0) { |
| 109 | + reverseArr.push(string.substr(i, wordLength + 1)); |
| 110 | + } else { |
| 111 | + wordLength++; |
| 112 | + } |
| 113 | + } |
| 114 | + return reverseArr.join(" "); |
| 115 | +} |
| 116 | + |
| 117 | +console.log(`Reverse dem words (harder way): ${reverseHard("i am ameiiizing")}`) |
| 118 | + |
| 119 | +/***********************************/ |
| 120 | +//turn a string of numbers to an int |
| 121 | +const stringToInt = (string) => { |
| 122 | + let newInt = 0; |
| 123 | + let digitCount = 0; |
| 124 | + let numberIsNeg = false; |
| 125 | + |
| 126 | + let i = 0; |
| 127 | + |
| 128 | + if (string.charAt(i) == "-") { |
| 129 | + numberIsNeg = true; |
| 130 | + i = 1; |
| 131 | + } |
| 132 | + |
| 133 | + //- "0" is suppose to make the string a number, but in javascript it isn't really necessary |
| 134 | + for (i; i < string.length; i++) { |
| 135 | + let numberInt = (string.charAt(i) - "0") * Math.pow(10, (string.length - i - 1)); |
| 136 | + newInt = newInt + numberInt; |
| 137 | + } |
| 138 | + |
| 139 | + if (numberIsNeg) { |
| 140 | + newInt *= -1; |
| 141 | + } |
| 142 | + |
| 143 | + return newInt; |
| 144 | +} |
| 145 | +console.log(`String to int: ${typeof stringToInt("1234")}`) |
| 146 | + |
| 147 | +/***********************************/ |
| 148 | +//turn an int to a string of numbers |
| 149 | + |
| 150 | +const intToString = (int) => { |
| 151 | + let newStr = ""; |
| 152 | + let digitCount = 0; |
| 153 | + let numberIsNeg = false; |
| 154 | + let temp = []; |
| 155 | + |
| 156 | + let i = 0; |
| 157 | + if (int < 0) { |
| 158 | + numberIsNeg = true; |
| 159 | + int *= -1; |
| 160 | + } |
| 161 | + |
| 162 | + if(int==0) { |
| 163 | + return "0"; |
| 164 | + } |
| 165 | + |
| 166 | + while (int != 0) { |
| 167 | + temp[i] = (Math.floor(int % 10)); |
| 168 | + int = (Math.floor(int / 10)); |
| 169 | + i++ |
| 170 | + } |
| 171 | + |
| 172 | + for (i; i > 0; i--) { |
| 173 | + newStr += temp[i-1]; |
| 174 | + } |
| 175 | + |
| 176 | + if (numberIsNeg) { |
| 177 | + newStr = "-" + newStr; |
| 178 | + } |
| 179 | + |
| 180 | +return newStr; |
| 181 | +} |
| 182 | +console.log(`Int to String: ${typeof intToString("1234")}`) |
| 183 | + |
| 184 | +/***********************************/ |
| 185 | +// Make a function that duplicates an array: |
| 186 | + |
| 187 | +//One Way: |
| 188 | +let duplicate = (nums) => { |
| 189 | + nums.forEach((value) => { |
| 190 | + nums.push(value); |
| 191 | + }) |
| 192 | + |
| 193 | + return nums; |
| 194 | + } |
| 195 | + |
| 196 | + console.log(duplicate([1, 2, 3, 4, 5])); // [1,2,3,4,5,1,2,3,4,5] |
| 197 | + |
| 198 | + //Second way: |
| 199 | + let duplicate2 = (arr) => { |
| 200 | + return arr.concat(arr); |
| 201 | + }; |
| 202 | + |
| 203 | + console.log(duplicate2([1, 2, 3, 4, 5])); // [1,2,3,4,5,1,2,3,4,5] |
0 commit comments