Find Missing Element in an Array of Numbers in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:47:46

1K+ Views

We are required to write a JavaScript function that takes in an array of numbers of length, say n. The array contains all the integers from 0 to n (including both 0 and n), but just one integer is missing, it can be any number and the array is not sorted. The task of our function is to find the missing number and return it in linear time and constant space.Since the array contains all the numbers from 0 to n but one, we can simply calculate the sum of all the elements of the array in linear time.And then ... Read More

Prime Digits Sum of a Number in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:46:36

332 Views

We are required to write a JavaScript function that takes in a number as the first and the only argument. The function should then sum all the digits of the number that are prime and return the sum as a number.For example −If the input number is −const num = 67867852;Then the output should be −const output = 21;because 7 + 7 + 5 + 2 = 21 −ExampleFollowing is the code −const num = 67867852; const sumPrimeDigits = (num) => {    const primes = '2357';    let sum = 0;    while(num){       const digit = ... Read More

Create Permutation of Array with Given Number of Elements in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:45:15

241 Views

We are required to write a JavaScript function that takes in an array of literals as the first argument and a number as the second argument.The function should construct an array of all such arrays which have the length equal to the number specified by the second argument and contains all possible permutations of the elements of the input array.For example −If the input array and the number are −const arr = ['k', 5]; const num = 3;Then the output should be −const output = [    [ 'k', 'k', 'k' ],    [ 'k', 'k', 5 ],    [ ... Read More

Adding Numbers Represented by String in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:43:24

815 Views

We are required to write a JavaScript function that takes in two strings, str1 and str2 that represents two numbers.Without converting the whole strings to respective numbers, our function should calculate the sum of those two string numbers and return the result as a string.For example −If the two strings are −const str1 = '234'; const str2 = '129';Then the output should be 363.−ExampleFollowing is the code −const str1 = '234'; const str2 = '129'; const addStringNumbers = (str1, str2) => {    let ind1 = str1.length - 1,    ind2 = str2.length - 1,    res = "",   ... Read More

Find the First Non-Repeating Character in a String using JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:41:53

501 Views

We are required to write a JavaScript function that takes in a string as the first and the only argument.The function should find and return the index of first character it encounters in the string which appears only once in the string.If the string does not contain any unique character, the function should return -1.For example −If the input string is −const str = 'hellohe';Then the output should be −const output = 4;ExampleFollowing is the code −const str = 'hellohe'; const firstUnique = (str = '') => {    let obj = {};    for(let i = 0; i < ... Read More

Finding Desired Numbers in a Sorted Array in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:40:01

219 Views

We have an array of integers which is sorted in the increasing order. We are required to write a JavaScript function that takes in one such array as the first argument and a target sum number as the second argument.The function should find and return two such numbers from the array that when added gives the target sum. The condition for solving this problem is that we have to do this in linear time and using constant space.ExampleFollowing is the code −const arr = [4, 6, 8, 9, 11, 12, 18, 21]; const num = 27; const findElements = (arr ... Read More

Find Length of Second Last Word in a Sentence in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:37:24

502 Views

A sentence is just a string which contains strings (called words) joined by whitespaces. We are required to write a JavaScript function that takes in one such sentence string and count the number of characters in the second to last word of the string. If the string contains no more than 2 words, our function should return 0.For example −If the input string is −const str = 'this is an example string';Then the output should be −const output = 7;because the number of characters in example is 7.ExampleFollowing is the code −const str = 'this is an example string'; const ... Read More

Finding Element Greater Than Its Adjacent Elements in JavaScript

AmitDiwan
Updated on 20-Jan-2021 06:35:53

524 Views

We are required to write a JavaScript function that takes in an array of numbers as the first and the only argument.The function should find and return one such number from the array which is greater than both, the number on its immediate right and the number on its immediate left. If there exists more than one such element in the array, our function should return any one of them.For example −If the input array is −const arr = [3, 6, 7, 9, 8, 2, 5];Then the output should be −const output = 9;Since the question demands finding the peak ... Read More

Use TensorFlow to Define Loss Function, Optimizer, and Train Model on IMDB Dataset in Python

AmitDiwan
Updated on 19-Jan-2021 13:44:59

175 Views

Tensorflow is a machine learning framework that is provided by Google. It is an open-source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.The ‘tensorflow’ package can be installed on Windows using the below line of code −pip install tensorflowThe ‘IMDB’ dataset contains reviews of over 50 thousand movies. This dataset is generally used with operations associated with Natural Language Processing.We are using the Google Colaboratory to run the below code. Google Colab or Colaboratory helps run Python code over the browser and requires zero ... Read More

Configure IMDB Dataset for Good Performance in TensorFlow

AmitDiwan
Updated on 19-Jan-2021 13:44:44

187 Views

Tensorflow is a machine learning framework that is provided by Google. It is an open−source framework used in conjunction with Python to implement algorithms, deep learning applications and much more. It is used in research and for production purposes.The ‘tensorflow’ package can be installed on Windows using the below line of code −pip install tensorflowTensor is a data structure used in TensorFlow. It helps connect edges in a flow diagram. This flow diagram is known as the ‘Data flow graph’. Tensors are nothing but multidimensional array or a list.The ‘IMDB’ dataset contains reviews of over 50 thousand movies. This dataset ... Read More

Advertisements