Front End Technology Articles - Page 312 of 745

How to get the most common values in array: JavaScript ?

AmitDiwan
Updated on 21-Nov-2020 09:34:54

477 Views

We are required to write a JavaScript function that takes in an array of literals that have repeating values. Our function should return an array of the most common element(s) in the array (if two or more elements appear for the same number of most times then the array should contain all those elements).ExampleThe code for this will be −const arr1 = ["a", "c", "a", "b", "d", "e", "f"]; const arr2 = ["a", "c", "a", "c", "d", "e", "f"]; const getMostCommon = arr => {    const count = {};    let res = [];    arr.forEach(el => {   ... Read More

Get intersection between two ranges in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:35:56

866 Views

Suppose, we have two arrays of numbers that represents two ranges like these −const arr1 = [2, 5]; const arr2 = [4, 7];We are required to write a JavaScript function that takes in two such arrays.The function should then create a new array of range, that is the intersection of both the input ranges and return that range.Therefore, the output for the above input should look like this −const output = [4, 5];ExampleThe code for this will be −const arr1 = [2, 5]; const arr2 = [4, 7]; const findRangeIntersection = (arr1 = [], arr2 = []) => {   ... Read More

How to create a third object from two objects using the key values in JavaScript?

AmitDiwan
Updated on 21-Nov-2020 09:33:39

238 Views

Suppose, we have two objects like these −const obj1 = {    positive: ['happy', 'excited', 'joyful'],    negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = {    happy: 6,    excited: 1,    unhappy: 3 };We are required to write a JavaScript function that takes in two such objects. The function should then use both these objects to calculate the positive and negative scores and return an object like this −const output = {positive: 7, negative: 3};ExampleThe code for this will be −const obj1 = {    positive: ['happy', 'excited', 'joyful'],    negative: ['depressed', 'sad', 'unhappy'] }; const obj2 = ... Read More

Group all the objects together having the same value for the '_id' key in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:33:44

386 Views

Suppose we have an array of objects like this −const arr = [    {_id : "1", S : "2"},    {_id : "1", M : "4"},    {_id : "2", M : "1"},    {_id : "" , M : "1"},    {_id : "3", S : "3"} ];We are required to write a JavaScript function that takes in one such array and groups all the objects together that have the same value for the '_id' key.Therefore, the final output should look something like this −const output = [    {_id : "1", M : "4", S : "2", ... Read More

Find the minimum and maximum values that can be calculated by summing exactly four of the five integers in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:32:20

2K+ Views

Given an array of five positive integers, we are required to find the minimum and maximum values that can be calculated by summing exactly four of the five integers.Then print the respective minimum and maximum values as a single line of two spaceseparated long integers.The array is not sorted all the times.For example −const arr = [1, 3, 5, 7, 9]The minimum sum is −1 + 3 + 5 + 7 = 16and the maximum sum is −3 + 5 + 7 = 24The return value of the function should be −[16, 24];ExampleThe code for this will be −const arr ... Read More

Store count of integers in order using JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:31:55

195 Views

Suppose, we have a long string that represents a number like this −const str = '11222233344444445666';We are required to write a JavaScript function that takes in one such string. Our function is supposed to return an object that should assign a unique "id" property to each unique number in the string and one other property "count" that stores the count of the number of times the number appears in the string.Therefore, for the above string, the output should look like −const output = {    '1': { id: '1', displayed: 2 },    '2': { id: '2', displayed: 4 }, ... Read More

Determine whether there is a pair of values in the array where the average of the pair equals to the target average in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:30:50

170 Views

We are required to write a JavaScript function that takes in an array of sorted integers and a target average as first and second arguments.The function should determine whether there is a pair of values in the array where the average of the pair equals to the target average.There's a solution with O(1) additional space complexity and O(n) time complexity. Since an array is sorted, it makes sense to have two indices: one going from begin to end (say y), another from end to begin of an array (say x).ExampleThe code for this will be −const arr = [1, 2, ... Read More

Filter unique array values and sum in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:30:24

761 Views

Suppose, we have an array of arrays like this −const arr = [[12345, "product", "10"], [12345, "product", "15"], [1234567, "other", "10"]];We are supposed to write a function that takes in one such array. Notice that all the subarrays have precisely three elements in them.Our function should filter out that subarray which has a repeating value as its first element. Moreover, for the subarray, we remove we should add their third element to its existing nonrepeating counterpart.So, for the above array, the output should look like −const output = [[12345, "product", "25"], [1234567, "other", "10"]];ExampleThe code for this will be −const ... Read More

Generate all combinations of supplied words in JavaScript

AmitDiwan
Updated on 11-Dec-2024 11:34:22

1K+ Views

In JavaScript, there are scenarios where you may need to generate every possible combination of a string's characters. This can be especially useful in areas like cryptography, analyzing subsets of data, or solving problems involving permutations. In this article, we’ll learn to implement a JavaScript function to achieve this task. Generate all combinations of supplied words Following are the different approaches for generating all combinations of supplied words − Recursive Approach Iterative Approach Using Bitmasking Using Recursive Approach The function iterates over each character in the string, appending ... Read More

The algorithm problem - Backtracing pattern in JavaScript

AmitDiwan
Updated on 21-Nov-2020 09:27:55

258 Views

Consider the following backtracing problem: On a 2−dimensional grid, there are 4 types of squares −1 represents the starting square. There is exactly one starting square.2 represents the ending square. There is exactly one ending square.0 represents empty squares we can walk over.−1 represents obstacles that we cannot walk over.We are required to write a function that returns the number of 4−directional walks from the starting square to the ending square, that walk over every non−obstacle square exactly once.Exampleconst arr = [    [1, 0, 0, 0],    [0, 0, 0, 0],    [0, 0, 2, -1] ]; const uniquePaths ... Read More

Advertisements