Posts

Showing posts from December, 2024

Different Array Methods in JavaScript

Hi there! Welcome to my blog. In this post, we will be looking into different kinds of array methods in JavaScript. Let's first see how to declare an array with 5 elements in JavaScript: const fruits = [ "Mango", "Apple", "Banana", "Orange", "Kiwi"]; Let's now understand the direction of array: End / Back of the array : In the above example, the element "Kiwi" is placed at end or back of the array. Front of the array: In the above example, the element "Mango" is placed at the front of the array. Let's now learn different array methods: push() - It adds one element to the end of the array. Every push operation makes changes in the original array. pop() - It removes the last element from an array and returns that element. Every pop operation makes changes in the original array. Either you can store the value returned from pop operation to a variable or just pop it. shift() - It removes the first element f...

[LeetCode] 58. Length of Last Word

Image
Problem Statement: Given a string s consisting of words and spaces, return the length of the last word in the string. Link:   https://leetcode.com/problems/length-of-last-word/description/ Brute-force Approach: Using .split() method, we will tokenize the string into an array. Using a loop, we will iterate through each element from the reverse. If length of the element is not equal to 0, return the length of the element. To handle edge cases where string is one letter only, we will return 1 . Brute-force Solution: Link:  https://gist.github.com/sumitkp11/57b3abecf29b780bfe8090a92b0828fc Brute-force Approach #2: Without using any in-built methods, we will traverse the string from reverse ignoring any whitespace. From the end, we will start the pointer and keep decrementing the pointer until we reach a character which is not a space. Once there, we will increment a length variable until we don't reach a space character. Approach #2 Solution:

[LeetCode] 219 - Contains Duplicate Number - II

Image
Problem Statement: Given an integer array nums and an integer k, return true if there are two distinct indices i and j in the array such that nums[i] == nums[j] and abs(i - j) <= k. Brute-force approach : Have a nested for loop that takes in all the pairs and check the above condition. Problems is that it will solve the basic test cases but since its time complexity is O(N^2), it will error out with " Time Exceeded " if an array is passed with 1000s of elements in it. Brute-force solution: Brute-force Result: Efficient Approach: With the help of a hash map we can track the last index of each element. In order to implement a hash map in JavaScript, we would use Map. We will first traverse through the array once, then check if the key exists in the map: If yes, then we will check if the current index and stored index is less or equal to 'k'. return true; If not, we will store the element as key and its index as value in Map. Outside the loop, ' return false ...

CSS Tools

CSS Validation Service:  https://jigsaw.w3.org/css-validator Color Picker Tool:  https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_colors/Color_picker_tool Box Shadow Generator:  https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Box-shadow_generator Border Image Generator:  https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Border-image_generator Border Radius Generator:  https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_backgrounds_and_borders/Border-radius_generator

[LeetCode] Missing Number

Image
Description: Given an array nums containing n distinct numbers in the range [0, n], return the only number in the range that is missing from the array. Brute-force Approach: Sort the array from 0 to N. Inside a simple for loop, iterate through every element in the array till the length of the array if the index and element doesn't match, return the index. Brute-force Solution: Brute-force Result: Efficient Approach: Since the numbers are from 0 to N then calculate the sum of all the numbers from 0 to N using the mathematical formula: ( N * ( N + 1 )) / 2 Calculate the sum of all numbers in the array. Subtract both sums with each other and return. Efficient Solution: Efficient Result:

[LeetCode] [136] Single Number Solution

 Problem Statement: Given a non-empty array of integers nums, every element appears twice except for one. Find that single one. You must implement a solution with a linear runtime complexity and use only constant extra space. Example 1: Input : nums = [2,2,1] Output : 1 Example 2: Input : nums = [4,1,2,1,2] Output : 4 Example 3: Input : nums = [1] Output : 1 Brute-force Approach (JavaScript): Create an object and store the digits in the array as key and the number of their occurrence as their value. Iterate thru the object and if value of any key is equal to 1, return key as a Number. Brute-force Solution: Efficient Approach: XOR or Exclusive OR has the following key properties: x ^ x = 0 x ^ 0 = x Efficient Solution:

[LeetCode] Longest Common Prefix

Image
 Hi, In this post we are going to be learning about how to solve the LeetCode Array Question: Longest Common Prefix. Problem Statement: Write a function to find the longest common string amongst an array of strings. If there is no common prefix, return an empty string. Test Case: Input: ["flower", "flow", "flight"] | Output: "fl" Input: ["a"] | Output: "a" Input: [""] | Output: "" Input: ["dog", "racecar", "car"] | Output: "" Approach - Vertical Scanning: We will compare all letters and then wherever we find any of them isn't matching, we stop and return the matching string. Algorithm: If length of whole array is 0, then return empty string. The outer loop runs from 0 to length of first element of the array while inner loop runs from 1 to length of the array. Visualize from the below image: JavaScript Code: Let me know in the comments how would you make it more ef...

Quickly shorten path in Linux terminal

 Copy and run the following command in your linux terminal: export PS1= "\[\e]0;\u@\h: \W\a\] ${debian_chroot:+($debian_chroot)} \[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\W\[\033[00m\]\$"

Promises Terms in JavaScript

Image
  Promise Chaining Instead of calling the second . then()  inside the handler for the first . then() , we can return the promise returned by json() , and call the second . then()  on that return value. Catching errors in Promise: To support error handling, Promise objects provides a . catch() method. It is very simple to remember: when asynchronous operation succeeds, . then()  is called whereas when it fails, the handler is passed to . catch() . If we add . catch() to the end of a promise chain, then it will be called when any one of the . then()  method fails in the chain. States of promise: pending : the promise has been created and the function has neither succeeded nor failed. fulfilled : the asynchronous function has succeeded. When a promise is fulfilled, its . then() handler is called. rejected : the asynchronous function has failed. When a promise is rejected, its . catch() handler is called. settled : to cover both fulfilled and rejected states. reso...