Algorithms
Leetcod 217: Contains Duplicate
neetcode/arrays-hashing/contains_duplicate.go Notes Problem LeetCode 217 - Contains Duplicate Solution Use a hash map to keep track of seen numbers. Iterate through the array once, checking if each number is already in the map. If it is, return true (duplicate found). If not, add the number to the map. If we finish iterating without finding a duplicate, return false. Analysis Time Complexity: O(n) Space Complexity: O(n) Code package arrayshashing func ContainsDuplicate(nums []int) bool { result := make(map[int]bool) for _, v := range nums { if _, exists := result[v]; exists { return true } result[v] = true } return false }
Leetcode 242: Valid Anagram
neetcode/arrays-hashing/valid_anagram.go Notes Problem LeetCode 242 - Valid Anagram Solution Use two hash maps to count the frequency of characters in each string. First, check if the lengths of the strings are equal. If not, return false. Then, iterate through both strings simultaneously, updating the character counts in their respective maps. Finally, compare the two maps. If they are identical, the strings are anagrams. Analysis Time Complexity: O(n), where n is the length of the strings Space Complexity: O(k), where k is the size of the character set (constant if considering only lowercase English letters)