title | tags | excerpt | dateModified | |
---|---|---|---|---|
JS Big-O Cheat Sheet |
|
Learn everything you need to know about Big-O notation with this handy cheatsheet. |
2023-01-08 05:00:00 -0400 |
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
/** | |
* Sorts an array using the heapsort algorithm. | |
* Heapsort is a comparison-based sorting algorithm. | |
* Heapsort is an improved selection sort consisting of the use of a | |
* heap data structure instead of a linear-time search to find the maximum or minimum element. | |
* | |
* @param {Array} arr - The array to be sorted. | |
* @return {Array} - The sorted array. | |
*/ | |
function heapsort(arr) { |
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
/** | |
* Generates a list of strings based on the given integer input. | |
* | |
* @param n the maximum integer value to generate strings for | |
* @return a list of strings containing the generated values | |
*/ | |
public List<String> fizzBuzz(int n) { | |
List<String> res = new ArrayList<>(n) | |
for (int i = 1; i <= n; i++) { | |
StringBuilder sb = new StringBuilder() |
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
let asciiValue: String = "1234567890ABCDEFGHIJ" | |
func asciiToHex(_ asciiValue: String) -> String { | |
let chars: [String.Element] = Array(asciiValue) | |
var hex: String = "" | |
for char: String.Element in chars { | |
hex += String(char.asciiValue!, radix: 16) | |
} | |
return hex | |
} |
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
// 17% faster than using filter on set | |
function difference(a, b) { | |
const s = new Set(b) | |
const result = [] | |
for (const x of a) { | |
if (!s.has(x)) { | |
result.push(x) | |
} | |
} |
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
/** | |
* Check if a given string is a palindrome. | |
* | |
* @param {string} str - The string to be checked. | |
* @return {boolean} Returns true if the string is a palindrome, false otherwise. | |
*/ | |
function isPalindrome(str) { | |
const formattedStr = str.toLowerCase().replace(/[\W_]/g, ""); | |
const reversedStr = formattedStr.split("").reverse().join(""); | |
return formattedStr === reversedStr; |
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
// This updated version should be more efficient due to the constant-time lookup in the dictionary, the use of StringBuilder for efficient string concatenation, and the iteration over the lookup keys in descending order. | |
func toRomanNumeral(_ num: Int) -> String { | |
let lookup: [Int: String] = [ | |
1000: "M", | |
900: "CM", | |
500: "D", | |
400: "CD", | |
100: "C", | |
90: "XC", | |
50: "L", |
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
func isPangram(string: String) -> Bool { | |
let alphabet = "abcdefghijklmnopqrstuvwxyz" | |
let lowerCaseString = string.lowercased() | |
for letter in alphabet { | |
if !lowerCaseString.contains(letter) { | |
return false | |
} | |
} | |
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
/** | |
* Checks if a string is a Pangram. | |
* | |
* @param {string} string - The string to be checked. | |
* @return {boolean} Returns true if the string is a pangram, false otherwise. | |
*/ | |
const isPangram = (string) => { | |
const alphabet = "abcdefghijklmnopqrstuvwxyz" | |
const lowerCaseString = string.toLowerCase() |
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
// Removed the localeCompare function call | |
// Replaced it with a more efficient comparison using the less-than and greater-than operators. | |
// This avoids the overhead of string comparison and should make the function faster. | |
/** | |
* Sorts an array of objects alphabetically based on a specified getter function. | |
* | |
* @param {Array} arr - The array to be sorted. | |
* @param {Function} getter - The getter function used to retrieve the value to compare. | |
* @param {string} [order="asc"] - The order in which to sort the array. Default is "asc" (ascending). |
NewerOlder