Skip to content

Instantly share code, notes, and snippets.

View Revivedaniel's full-sized avatar

Daniel Stark Revivedaniel

View GitHub Profile

Binary Search Algorithm

Given I have a sorted array, I want to find the index of a number within that array.

binarySearch(arr, target)

This algorithm will accept a sorted array and a target value. The array will only contain integers. The target might not be in the array, if this is the case return -1. The array will never be empty.

Examples

Same Frequency(int) Algorithm using the Frequency Counter method

Given I have two numbers, I want to know if they not only have the same numbers, but also if they appeare the same amount of times in the numbers.

sameFrequency(num1, num2)

The numbers passed through will always be positive. The numbers passed through will always be whole numbers. There will only ever be two numbers passed through.

Examples

Longest Distinct Substring Algorithm using the Sliding Window method

Given I have a string, I want to know what the length of the longest substring containing distinct characters.
In other words, I want to know the length of the largest set of characters in a row where the characters are not duplicates.

longestDistinctSubstring(str)

  • The string passed into the function will only ever contain lowercase letters with no spaces.
  • The string might be empty.
  • In the case where the string is empty, return 0 as there are no characters.
  • In the case where all the letters are the same in the string, return 1 as there is only 1 unique letter.

Min Subarray Length Algorithm using the Sliding Window method

Given I have an array of integers, I want to determine the shortest subarray length where the sum of all the values in the subarray are equal to or greater than the number provided

minSubArrayLen(arr, targetSum)

The array passed through will only ever contain integers (Positive and Negative whole numbers including 0).
The array passed through will never be empty.
The number passed through will always be positive(Greater than 0).
The number passed through might be longer than the array's length.
In the case where the number is larger than the array length, return 0.

Examples

Max Subarray Sum Algorithm using the Sliding Window method

Give I have an array of integers, I want to determine the largest sum of a consecutive sub set of those integers.

maxSubarraySum(arr, num)

The array passed through will only ever contain integers (Positive and Negative whole numbers including 0). The array passed through will never be empty. The number passed through will always be positive(Greater than 0). The number passed through might be longer than the array. In the case where the number is larger than the array length, return null.

Unique Value Counter Algorithm using the Multiple Pointer method

Given I have a sorted array of numbers, I want to know how many unique values are within that array.

countUniqueValues(arr)

The array passed into the function will always be a sorted list of numbers. The numbers can be negative, zero, or positive. The algorithm should return the number of unique values in the array. An empty array should be expected to be passed into the algorithm. In the case of an empty array, it should return 0 since there are no values at all.

Anagram Algorithm using the Frequency Counter method

An anagram is a word or phrase formed by rearranging the letters of another word or phrase. In other words, it's a type of wordplay where the letters of a word or phrase are rearranged to create a new word or phrase that has a different meaning. Here are some examples:

  • "listen" and "silent"
  • "elbow" and "below"
  • "night" and "thing"
  • "debit card" and "bad credit"
  • "astronomer" and "moon starer"
@Revivedaniel
Revivedaniel / Regex-URL-Validation.md
Last active February 25, 2022 18:21
A short breakdown of the Regex used for general URL Validation

Regex URL Validation

In this article we are going to talk about Regex and URL validation. We will be going over everthing from Anchors to Character Escapes and the examples are in JavaScript.

Summary

If Regex is new to you. Regex is used in many programming languages to distinguish patterns within strings. Each character in Regex is used to represent a pattern within the string being searched. Patterns are made up of normal and special characters. Normal characters like abc 123 represent themselves while special characters like /.$ have special meaning.

For example, the following code will check if a string is exactly abc.