Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Object Oriented Programming Articles
Page 50 of 589
Implementing Heap Sort using vanilla JavaScript
Heap Sort is a comparison-based sorting algorithm that can be thought of as an improved selection sort. Like selection sort, it divides input into sorted and unsorted regions, but uses a binary heap data structure to efficiently find the maximum (or minimum) element. How Heap Sort Works Heap Sort works in two main phases: Build Max Heap: Convert the array into a max heap where parent nodes are larger than their children Extract Elements: Repeatedly remove the maximum element (root) and restore the heap property Implementation Here's a complete implementation of Heap Sort ...
Read MorePrefix calculator using stack in JavaScript
A Reverse Polish Notation (RPN) calculator processes operators after their operands, using a stack data structure. This implementation evaluates mathematical expressions efficiently without parentheses. How RPN Works In RPN, operators follow their operands. For example, "3 4 +" means "3 + 4". The algorithm uses a stack: Push operands onto the stack When encountering an operator, pop two operands, perform the operation, and push the result back The final stack contains the result Step-by-Step Process Consider the input array: const arr = [1, 5, '+', 6, 3, '-', '/', 7, '*']; ...
Read MoreExpressive words problem case in JavaScript
Sometimes people repeat letters to represent extra feeling, such as "hello" → "heeellooo", "hi" → "hiiii". In these strings like "heeellooo", we have groups of adjacent letters that are all the same: "h", "eee", "ll", "ooo". For some given string S, a query word is stretchy if it can be made to be equal to S by any number of applications of the following extension operation: choose a group consisting of characters c, and add some number of characters c to the group so that the size of the group is 3 or more. For example, starting with ...
Read MorePossible combinations and convert into alphabet algorithm in JavaScript
In JavaScript, we can solve the alphabet decoding problem where each letter maps to a number (a=1, b=2, ..., z=26). Given an encoded message, we need to count how many different ways it can be decoded back into letters. For example, the message '111' can be decoded as 'aaa' (1-1-1), 'ka' (11-1), or 'ak' (1-11), giving us 3 possible combinations. Understanding the Problem The challenge is that some digit sequences can be interpreted in multiple ways: Single digits 1-9 can be decoded as letters a-i Two-digit combinations 10-26 can be decoded as letters j-z We ...
Read MoreProgram to make vowels in string uppercase and change letters to next letter in alphabet (i.e. z->a) in JavaScript
We need to write a JavaScript function that takes a string and performs two transformations: convert vowels to uppercase and shift each letter to the next letter in the alphabet (with 'z' wrapping to 'a'). The function should construct a new string based on the input string where all vowels are uppercased and each alphabet character moves to the next letter in sequence. Problem Understanding For example, if the input string is: const str = 'newString'; The expected output should be: const output = 'oExSusIoh'; Here's how the transformation ...
Read MoreHow to store two arrays as a keyvalue pair in one object in JavaScript?
Suppose, we have two arrays of literals of same length like these − const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; const arr2 = ['Rahul', 'Sharma', 23, 'Tilak Nagar', false]; We are required to write a JavaScript function that takes in two such arrays. The function should construct an object mapping the elements of the second array to the corresponding elements of the first array. Method 1: Using Array.reduce() We will use the Array.prototype.reduce() method to iterate over the arrays, building the object. const arr1 = ['firstName', 'lastName', 'age', 'address', 'isEmployed']; ...
Read MoreFinding the longest "uncommon" sequence in JavaScript
We are required to write a JavaScript function that takes in an array of strings. The function should find the longest uncommon subsequence among the strings of the array. By longest uncommon subsequence we mean the longest subsequence of one of these strings and this subsequence should not be any subsequence of the other strings. Our function should return the length of this longest uncommon subsequence. For example: If the input array is − const arr = ["aba", "cdc", "eae"]; Then the output should be 3. Understanding the Problem The key ...
Read MoreFinding product of an array using recursion in JavaScript
We are required to write a JavaScript function that takes in an array of numbers. Our function should do the following two things: Make use of a recursive approach. Calculate the product of all the elements in the array. And finally, it should return the product. For example, if the input array is: const arr = [1, 3, 6, 0.2, 2, 5]; Then the output should be: 36 How Recursion Works Recursion breaks down the problem into smaller subproblems. For ...
Read MoreFind what numbers were pressed to get the word (opposite of phone number digit problem) in JavaScript
The mapping of the numerals to alphabets in the old keypad type phones used to be like this: const mapping = { 1: [], 2: ['a', 'b', 'c'], 3: ['d', 'e', 'f'], 4: ['g', 'h', 'i'], 5: ['j', 'k', 'l'], 6: ['m', 'n', 'o'], 7: ['p', 'q', 'r', 's'], 8: ['t', 'u', 'v'], 9: ['w', 'x', 'y', 'z'] }; console.log(mapping); { '1': [], '2': [ 'a', 'b', 'c' ], '3': ...
Read MoreAre the strings anagrams in JavaScript
Two strings are said to be anagrams of each other if by rearranging, rephrasing or shuffling the letters of one string we can form the other string. Both strings must contain exactly the same characters with the same frequency. For example, 'something' and 'emosghtin' are anagrams of each other because they contain the same letters with the same count. We need to write a JavaScript function that takes two strings and returns true if they are anagrams of each other, false otherwise. Method 1: Character Frequency Count This approach counts the frequency of each character in ...
Read More