Brute Force - Linear Search ./ARRAY/BFcontainsDuplicate.js
// Brute // Time O(n) | Space O(1)// https://leetcode.com/problems/contains-duplicate/// Param {number[]} nums // Return {boolean}constcontainsDuplicate=nums=>{for(letright=0;right<nums.length;right++){for(letleft=0;left<right;left++){constisDuplicate=nums[left]===nums[right];if(isDuplicate)returntrue}}returnfalse;}console.log(containsDuplicate([1,2,3,4,5]));// output: falseconsole.log(containsDuplicate([1,1,2,3,4,5]));// output: true
We declare a variable called right and initialize it to 0 because we need to keep track of the current index of the rightmost element in the array. We start at 0 because we know that the first element in the array is not a duplicate of itself.
We create a loop that iterates from 0 to the length of the array because we need to compare each element in the array to all of the elements that come after it.
We declare a variable called left and initialize it to 0 because we need to keep track of the current index of the leftmost element in the array. We start at 0 because we know that the first element in the array is not a duplicate of itself.
We create a nested loop that iterates from 0 to right because we need to compare the current element to all of the elements that come before it.
We compare the values of the left and right variables because if they are equal, then the element at the left index is a duplicate of the element at the right index.
5.If a duplicate is found, we return true because this indicates that the array contains at least one duplicate element.
After the outer loop has finished iterating, we return false because this indicates that no duplicates were found in the array.
Sort - HeapSort Space O(1) | QuickSort Space O(log(N)) ./ARRAY/hashSetContainsDuplicate.js
// Sort - HeapSort Space O(1) | QuickSort Space O(log(N))// Time O(N * log(N)) | Space O(1)// https://leetcode.com/problems/contains-duplicate/// @param {number[]} nums// @return {boolean}constcontainsDuplicate=nums=>{nums.sort((a,b)=>a-b);//Time O(N * log(N)) | Space O(1 || log(N))returnhasDuplicates(nums);}consthasDuplicates=nums=>{for(letcurr=0;curr<(nums.length-1);curr++){//Time O(N) constnext=(curr+1);constisNextDuplicate=nums[curr]===nums[next];if(isNextDuplicate)returntrue;}returnfalse}console.log(containsDuplicate([1,2,3,4,5]));// output: falseconsole.log(containsDuplicate([1,1,2,3,4,5]));// output: true
We sort the array because this will allow us to easily compare each element to the elements that come after it.
We create a function called hasDuplicates because this will allow us to focus on the task of checking for duplicates in the sorted array.
We create a loop that iterates from 0 to the length of the array minus 1 because we need to compare each element in the array to the element that comes after it.
We compare the values of the current element and the next element because if they are equal, then the element at the current index is a duplicate of the element at the next index.
If a duplicate is found, we return true because this indicates that the sorted array contains at least one duplicate element.
After the loop has finished iterating, we return false because this indicates that no duplicates were found in the sorted array.
Hash Set
// Hash Set // Time O(N) | Space O(N)// @param {number[]} nums// @return {boolean}constcontainsDuplicate=nums=>{constnumSet=newSet(nums);constisEqual=numSet.size===nums.lengthreturn!isEqual;}console.log(containsDuplicate([1,2,3,4,5]));// output: falseconsole.log(containsDuplicate([1,1,2,3,4,5]));// output: true
Create a new Set object and pass the array nums as an argument. This will create a set that contains the unique elements of the array.
Get the size of the Set object. This will tell us how many unique elements are in the array.
Compare the size of the Set object to the length of the array. If the sizes are not equal, then the array must contain at least one duplicate element.
Return the opposite of the comparison result. This will return true if the array contains at least one duplicate element, and false otherwise.
Hash Set - Early Exit
// Hash Set - Early Exit// Time O(N) | Space O(N)// https://leetcode.com/problems/contains-duplicate/// @param {number[]} nums// @return {boolean}constcontainsDuplicate=(nums,numSet=newSet())=>{for(numofnums){// Time O(N)if(numSet.has(num))returntrue;numSet.add(num);// Space O(N)}returnfalse;}console.log(containsDuplicate([1,2,3,4,5]));// output: falseconsole.log(containsDuplicate([1,1,2,3,4,5]));// output: true
Create a new Set object and pass the array nums as an argument. This will create a set that contains the unique elements of the array.
Iterate over the array nums
Check if the current element is in the Set object. If it is, then return true
Add the current element to the Set object
Continue iterating until the end of the array.
Return false if no duplicates were found.
isAnagram()
Sort - HeapSort Space O(1) | QuickSort Space O(log(N))
The function isAnagram takes two parameters s and t.
It first checks if the length of the two strings is equal.
If not, it returns false.
If the length is equal, it calls the function reorder on both strings and compares the result
The function reorder takes one parameter str.
It splits the string into an array of characters using the split() method.
It then sorts the array in alphabetical order using the sort() method.
Finally, it joins the sorted array back into a string using the join() method.