Found 9316 Articles for Object Oriented Programming

Detecting the first non-unique element in array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:53:28

107 Views

We are required to write a function that returns the index of the very first element that appears at least twice in the array. If no element appears more than once, we have to return -1. We have to do this in constant space (i.e., without utilizing extra memory).So, let's write the solution for this problem.We will use a for loop to iterate over the array and use the Array.prototype.lastIndexOf() method to check for duplicacy.ExampleThe code for this will be −const arr1 = [0, 1, 1, 2, 3, 4, 4, 5]; const firstRedundant = arr => {    for(let i ... Read More

Using recursion to remove consecutive duplicate entries from an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:51:32

369 Views

We are supposed to write a function that takes in an array of number/string literals. The function should remove all the redundant consecutive elements of the array without using extra memory space.For example, if the input array is −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1];Then the output should be −const output = [17, 12, 354, 1];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [17, 17, 17, 12, 12, 354, 354, 1, 1, 1]; const comp = (arr, len = 0, deletable = false) => { ... Read More

Combining the identical entries together in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:49:38

55 Views

We have an array of numbers that have got identical entries. We are required to write a function that takes in the array and groups all the identical entries into one subarray and returns the new array thus formed.For example: If the input array is −const arr = [234, 65, 65, 2, 2, 234];// then the output should be −const output = [[234, 234], [65, 65], [2, 2]];We will use a Hashmap to keep a track of the elements already occurred and iterate over the array using a for loop.Therefore, let’s write the code for this function −ExampleThe code for ... Read More

Array flattening using loops and recursion in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:48:03

487 Views

We are required to write a JavaScript array function that takes in a nested array with false values as well and returns an array with all the elements present in the array without any nesting.For example: If the input is −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], [6]];Then the output should be −const output = [1, 2, 3, 4, 5, false, 6, 5, 8, null, 6];Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr = [[1, 2, 3], [4, 5, [5, false, 6, [5, 8, null]]], ... Read More

If the element repeats, remove all its instances from array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:46:45

66 Views

We are required to write a function that takes in an array and returns a new array that have all duplicate values removed from it. The values that appeared more than once in the original array should not even appear for once in the new array.For example, if the input is −const arr = [763, 55, 43, 22, 32, 43, 763, 43];The output should be −const output = [55, 22, 32];Array.prototype.indexOf(): It returns the index of first occurrence of searched string if it exists, otherwise -1.Array.prototype.lastIndexOf(): It returns the index of last occurrence of searched string if it exists, otherwise ... Read More

Keeping only alphanumerals in a JavaScript string in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:43:21

68 Views

We are required to write a JavaScript function that takes in a string that might contain some special characters.The function should return a new string should have all special characters replaced with their corresponding ASCII value.Therefore, let’s write the code for this function −ExampleThe code for this will be −const str = 'Th!s !s @ str!ng th@t cont@!ns some special characters!!'; const specialToASCII = str => {    let res = '';    for(let i = 0; i < str.length; i++){       if(+str[i] || str[i].toLowerCase() !== str[i].toUpperCase() || str[i] === ' '){          res += ... Read More

Listing all the prime numbers upto a specific number in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:41:38

136 Views

We are required to write a JavaScript function that takes in a number, say n, and returns an array containing all the prime numbers upto n.For example: If the number n is 24.Then the output should be −const output = [2, 3, 5, 7, 11, 13, 17, 19, 23];Therefore, let’s write the code for this function −ExampleThe code for this will be −const num = 24; const isPrime = num => {    let count = 2;    while(count < (num / 2)+1){       if(num % count !== 0){          count++;          continue;       };       return false;    };    return true; }; const primeUpto = num => {    if(num < 2){       return [];    };    const res = [2];    for(let i = 3; i

AND product of arrays in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:40:10

86 Views

We have an array of arrays of boolean like this −const arr = [[true, false, false], [false, false, false], [false, false, true]];We are required to write a function that merges this array of arrays into a one-dimensional array by combining the corresponding elements of each subarray using the AND (&&) operator.Let’s write the code for this function. We will be using Array.prototype.reduce() function to achieve this.ExampleThe code for this will be −const arr = [[true, false, false], [false, false, false], [false, false, true]]; const andMerge = (arr = []) => {    return arr.reduce((acc, val) => {       ... Read More

Deviations in two JavaScript arrays in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:38:52

107 Views

We have two arrays of numbers like these −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34];We are required to write a JavaScript function that takes in two such arrays and returns the element from arrays that are not common to both.Therefore, let’s write the code for this function −ExampleThe code for this will be −const arr1 = [12, 54, 2, 4, 6, 34, 3]; const arr2 = [54, 2, 5, 12, 4, 1, 3, 34]; const difference = (first, second) => {    const res = []; ... Read More

Upper or lower elements count in an array in JavaScript

AmitDiwan
Updated on 20-Oct-2020 11:37:33

73 Views

Consider we have an array of Numbers that looks like this −const array = [54, 54, 65, 73, 43, 78, 54, 54, 76, 3, 23, 78];We are required to write a function that counts how many of the elements are in the array below / above a given number.For example, if the number is 60 −The answer should be 5 elements below it (54, 54, 43, 3, 23) and 5 element par it (65, 73, 78, 76, 78) Therefore, let’s write the code for this function −ExampleThe code for this will be −const array = [54, 54, 65, 73, 43, ... Read More

Advertisements