{"id":135897,"date":"2022-09-06T11:38:57","date_gmt":"2022-09-06T11:38:57","guid":{"rendered":"https:\/\/www.sushilkumar.ind.in\/blog\/?page_id=135897"},"modified":"2024-08-13T14:03:47","modified_gmt":"2024-08-13T14:03:47","slug":"js-interview-algorithm-question-answers","status":"publish","type":"page","link":"https:\/\/www.sushilkumar.ind.in\/blog\/","title":{"rendered":"The Ultimate Guide to JavaScript Interview Algorithms and Solutions"},"content":{"rendered":"\n<p><a href=\"#1\">1. Find a duplicate number in an array of integers in JavaScript.<\/a><\/p>\n<p><a href=\"#2\">2. Find the missing number in a given integer Array in JavaScript.<\/a><\/p>\n<p><a href=\"#3\">3. Find the largest and smallest number in an unsorted array of integers in JavaScript.<\/a><\/p>\n<p><a href=\"#4\">4. Return an array showing the cumulative sum at each index of an array of integers in JavaScript.<\/a><\/p>\n<p><a href=\"#5\">5. Find all duplicate numbers in an array with multiple duplicates in JavaScript.<\/a><\/p>\n<p><a href=\"#6\">6. Remove all duplicates from an array of integers in JavaScript.<\/a><\/p>\n<p><a href=\"#7\">7. Find all pairs in an array of integers whose sum is equal to a given number in JavaScript.<\/a><\/p>\n<p><a href=\"#8\">8. Replace One Character With Another in JavaScript.<\/a><\/p>\n<p><a href=\"#9\">9. Reverse String in JavaScript.<\/a><\/p>\n<p><a href=\"#10\">10. Remove all even integers from an array in JavaScript.<\/a><\/p>\n<p><a href=\"#11\">11. Check and Verify a word as palindrome in JavaScript.<\/a><\/p>\n<p><a href=\"#12\">12. How to find the factorial of a Number in JavaScript?<\/a><\/p>\n<p><a href=\"#13\">13. Find the all duplicate characters or number with index(count) from the input string in javascript.<\/a><\/p>\n<p><a href=\"#14\">14. How to check prime number in JavaScript?<\/a><\/p>\n<p><a href=\"#15\">15. How to check Prime Factors in JavaScript?<\/a><\/p>\n<p><a href=\"#16\">16. How do get nth Fibonacci number?<\/a><\/p>\n<p><a href=\"#17\">17. How do Swap number without temp?<\/a><\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"1\">1. Find a duplicate number in an array of integers<\/h3>\n\n\n\n<h4 class=\"wp-block-heading\">Solutions-1<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1,2,3,4,5,6,7,7,8,6,10];\nconst findDupes = (arr) =&gt; {\n  const observed = {};\n  for(let i = 0; i &lt; arr.length; i++) {\n    if(observed&#91;arr&#91;i]]) {\n      return arr&#91;i]\n    } else {\n      observed&#91;arr&#91;i]] = arr&#91;i];\n    }\n  }\n  \n  return false;\n}\nconsole.log(findDupes(arr)); \/\/ Returns 7<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Solution- 2<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>const array = &#91;1, 2, 3, 4, 5, 3, 4, 52, 356, 3, 4, 5, 6];\n\nconst duplicates = array.filter((value, index) =&gt; {\n  return array.indexOf(value) !== index &amp;&amp; array.lastIndexOf(value) === index;\n});\n\nconsole.log(\"Duplicate numbers:\", duplicates); \/\/ Output: Duplicate numbers: &#91;3, 4, 5]\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Solution-3<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"2\">2. Find the missing number in a given integer Array.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;1,2,3,4,5,6,7,8,10];\nconst findMissingNum = (arr) =&gt; {\n  for(var i = 0; i &lt; arr.length - 1; i++) {\n    if(arr&#91;i] + 1 != arr&#91;i+1] ) {\n      return arr&#91;i] + 1;\n    }\n  }\n  \n  return false;\n}\nconsole.log(findMissingNum(arr)); \/\/ Returns 9, the missing number<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"3\">3. Find the largest and smallest number in an unsorted array of integers.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, 2, 3, 4, 100];\nconst findMaxMin = (arr) =&gt; {\n  let max = arr&#91;0];\n  let min = arr&#91;0];\n  \n  for(let i = 0; i &lt; arr.length; i++) {\n    if(arr&#91;i] &gt; max) {\n      max = arr&#91;i];\n    } else if (arr&#91;i] &lt; min) {\n      min = arr&#91;i];\n    }\n  }\n  \n  return {\n    \"max\": max,\n    \"min\": min\n  };\n}\nconsole.log(findMaxMin(arr)); \/\/ Returns object { \"max\": 100, \"min\": 1 }<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"4\">4. Return an array showing the cumulative sum at each index of an array of integers.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;1,3,5,7];\nconst cumulativeSum = list =&gt; {\n  let result = &#91;list&#91;0]];\n  \n  for(let i = 1; i &lt; list.length; i++) {\n    result.push(list&#91;i] + result&#91;i-1]);\n  } \n  \n  return result;\n}\nconsole.log(cumulativeSum(arr)); \/\/ Returns &#91;1, 4, 9, 16]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"5\">5. Find all duplicate numbers in an array with multiple duplicates<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1,1,2,3,4,5,6,7,8,6,6,7,7,7,10,10];\nconst returnMultipleDupesArray = (arr) =&gt; {\n  let observed = {};\n  let dupesArray = &#91;];\n  \n  for(let i = 0; i &lt; arr.length; i++) {\n \n    if(observed&#91;arr&#91;i]]) {\n      if(observed&#91;arr&#91;i]] === 1) {\n        dupesArray.push(arr&#91;i]);\n      }\n      \n      observed&#91;arr&#91;i]] = observed&#91;arr&#91;i]] + 1;\n    } else {\n      observed&#91;arr&#91;i]] = 1;\n    }\n  }\n  \n  return dupesArray;\n}\nconsole.log(returnMultipleDupesArray(arr)); \/\/ prints &#91;1, 6, 7, 10]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"6\">6. <strong>Remove all duplicates from an array of integers.<\/strong><\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution-1<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\"><code>const arr = [1, 1, 1, 1, 1, 1, 1];const removeDupes = (arr) =&gt; {\n  let result = [];\n  let previous = arr[0];\n  result[0] = previous;\n  \n  for(let i = 0; i &lt; arr.length; i++) {\n    \n    if (arr[i] != previous) {\n      result.push(arr[i]);\n    }\n    \n    previous = arr[i];\n  }\n  \n  return result;\n}\nconsole.log(removeDupes(arr)); \/\/ Output  prints [1]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution-2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, 2, 3, 4, 5, 6, 7, 7, 8, 6, 10];\n\nconst uniqueValues = &#91;...new Set(arr)];\n\nconsole.log(uniqueValues); \/\/ Output: &#91;1, 2, 3, 4, 5, 6, 7, 8, 10]\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"7\">7. Find all pairs in an array of integers whose sum is equal to a given number.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let arr = &#91;1,5,6,1,0,1];\nconst findSumPairs = (arr, value) =&gt; {\n  let sumsLookup = {};\n  let output = &#91;];\n  \n  for(let i = 0; i &lt; arr.length; i++) {\n    let targetVal = value - arr&#91;i];\n    \n    if(sumsLookup&#91;targetVal]) {\n      output.push(&#91;arr&#91;i], targetVal]);\n    }  \n    \n    sumsLookup&#91;arr&#91;i]] = true;\n  }\n  \n  return output;\n}\nconsole.log(findSumPairs(arr, 6));\n\n\/\/ Output &#91; &#91; 5, 1 ], &#91; 1, 5 ], &#91; 0, 6 ], &#91; 1, 5 ] ]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"8\">8. Replace One Character With Another.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function replaceChar(inputStr, replaceThis, withThis) {\n    var retval = &#91;];\n\n    for (var i = 0; i &lt; inputStr.length; i++) {\n        if (inputStr&#91;i] === replaceThis) {\n            retval.push(withThis);\n        } else {\n            retval.push(inputStr&#91;i]);\n        }\n    }\n\n    return retval.join('');\n}\n\nconsole.log(replaceChar('hello world', 'l', 'X')); \/\/Output heXXo worXd\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9\">9. How to Reverse a String?<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function reverseString(inputStr) {\n    var retval = &#91;];\n    var tokens = inputStr.split('');\n\n    for (var i = tokens.length - 1; i &gt;= 0; i--) {\n        retval.push(inputStr&#91;i]);\n    }\n\n    return retval.join('');\n}\n\nconsole.log(reverseString('Hello World')); \/\/ Output dlroW olleH<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function reverseString(str) {\n    return str.split(\"\").reverse().join(\"\");\n}\nconsole.log(reverseString(\"hello\")); \/\/ output: olleh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 3  ES6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>let string = \"Hello\"\nstring = &#91;...string].reverse().join(\"\");\n\nconsole.log(string); \/\/ output : olleh<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 4  ES6<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function reverseWords(input) {\n    \/\/ Split the input string into words, reverse each word, and join them back into a string\n    return input.split(' ').map(word =&gt; word.split('').reverse().join('')).join(' ');\n}\n\nvar input = \"sushil kumar\";\nvar output = reverseWords(input);\nconsole.log(output); \/\/ Output: \"lihsus ramuk\"<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"10\">10. Remove all even integers from an array.<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>const inputData = &#91;4, 1, 9, 10, 15, 22, 5, 14]\nremoveEvenNumbers (inputData) =&gt; {\n    let odds = &#91;]\n    for (let number of inputData) {\n        if (number % 2 != 0) odds.push(number)\n    }\n  return odds\n}\nconsole.log(removeEvenNumbers(inputData)))\n\/\/ Output &#91;4, 10, 22, 14]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>removeEvenNumbers(inputData) =&gt; {\n    return inputData.filter((number =&gt; (number % 2) != 0))\n}\nconsole.log(removeEvenNumbers(inputData))<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"11\">11. Check and Verify a word as Palindrome.<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function isPalindrome(str){\n    var i, len = str.length;\n    for(i =0; i&lt;len\/2; i++){\n      if (str&#91;i]!== str&#91;len -1 -i]) \/\/ str&#91;0] !== str&#91;3] i.e., r !== r\n         return false;\n    }\n    return true;\n  }\n  \n  console.log(isPalindrome('radar')); \/\/ Output return true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function isPalindrome(s) {\n    \/\/ Reverse the string\n    let reversed = s.split('').reverse().join('');\n    \/\/ Check if the reversed string is equal to the original (case-sensitive)\n    return s === reversed;\n}\n\nlet strings = &#91;'ab', 'ba', 'bv'];\n\nstrings.forEach(string =&gt; {\n    if (isPalindrome(string)) {\n        console.log(`${string} is a palindrome.`);\n    } else {\n        console.log(`${string} is not a palindrome.`);\n    }\n});\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"12\">12. How to find the factorial of a Number.<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution-1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code> function factorialNumber(n){\n    let answer = 1;\n    if (n == 0 || n == 1){\n      return answer;\n    }else{\n      for(var i = n; i &gt;= 1; i--){\n        answer = answer * i;\n      }\n      return answer;\n    }  \n  }\n  let n = 5;\n  answer = factorialNumber(n)\n  console.log(\"The factorial  Number of \" + n + \" is \" + answer);\n\/\/ output is 120.<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution-2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ Function to calculate factorial\nconst factorial = n =&gt; {\n    if (n === 0) {\n        return 1;\n    }\n    return n * factorial(n - 1);\n};\n\n\/\/ Higher-order function to check if a number is a factorial number\nconst isFactorialNumber = num =&gt; {\n    \/\/ Generate factorials until they exceed num\n    const factorials = Array.from({ length: num }, (_, i) =&gt; factorial(i));\n    \n    \/\/ Check if num exists in the generated factorials array\n    return factorials.includes(num);\n};\n\n\/\/ Example usage:\nconsole.log(isFactorialNumber(24)); \/\/ true, since 24 = 4!\nconsole.log(isFactorialNumber(25)); \/\/ false\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"13\">13. Find the all duplicate characters or number with index(count) from the input string.<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function findDuplicatesCharWithNumber(str)\n{\n    var count = {};\n    for (var i = 0; i &lt; str.length; i++) {\n        count&#91;str&#91;i]] = 0;\n        \n    }\n     \n    for (var i = 0; i &lt; str.length; i++) {\n        count&#91;str&#91;i]]++;\n    }\n \n    for (var ele in count) {\n        if (count&#91;ele] &gt; 1)\n            console.log(`${ele}=${count&#91;ele]}`);\n    }\n}\n\n\/\/ for number\nvar str = \"23232232093239230923\";\nfindDuplicatesCharWithNumber(str);    \/\/ output is 0=2, 2=8, 3=7, 9=3\n\/\/ for string\nvar str = \"abscdertnsjmmajmansanasnanma\";\nfindDuplicatesCharWithNumber(str);  \/\/ output is a=7, s=4, n=5, j=2, m=4\n\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"14\">14. How to check prime number in JavaScript?<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function isPrime(n)\n{\n\n  if (n===1)\n  {\n    return false;\n  }\n  else if(n === 2)\n  {\n    return true;\n  }\n  else\n  {\n    for(var x = 2; x &lt; n; x++)\n    {\n      if(n % x === 0)\n      {\n        return false;\n      }\n    }\n    return true;  \n  }\n}\nconsole.log(isPrime(13));  \/\/ Output: true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfunction isPrime(n){\n  var divisor = 2;\n\n  while (n &gt; divisor){\n    if(n % divisor == 0){\n     return false; \n    }\n    else\n      divisor++;\n  }\n  return true;\n}\n\nconsole.log(isPrime(13));\n \/\/ Output= true<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"15\"> 15. How to check Prime Factors in JavaScript?<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function primeFactors(n){\n   var factors = &#91;], \n       divisor = 2;\n   \n   while(n&gt;2){\n     if(n % divisor == 0){\n        factors.push(divisor); \n        n= n\/ divisor;\n     }\n     else{\n       divisor++;\n     }     \n   }\n   return factors;\n }\n \nconsole.log(primeFactors(105));  \/\/ Output: &#91; 3, 5, 7 ]<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"16\">16. How do get nth Fibonacci series number?<\/h3>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1 for Fibonacci series<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>\nfunction fibonacciSeries(n){\n  var fibonac = &#91;0, 1];\n  \n  if (n &lt;= 2) return 1;\n\n  for (var i = 2; i &lt;=n; i++ ){\n    fibonac&#91;i] = fibonac&#91;i-1]+fibonac&#91;i-2];\n    console.log(fibonac&#91;i]); \/\/ Print here Fibonacci series: 1\n 2 3 5 8 13 21 34 55\n  }\n\n return fibonac&#91;n];\n} \n\nconsole.log(fibonacciSeries(10));  \/\/ Output is 55\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"17\">17. How to swap two numbers without using a temporary variable?<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 1<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function swapNumb(a, b){\n  console.log('Before swap Output: ','a: ', a, 'b: ', b);\n\/\/ Before swap Output:  a:  2 b:  3\n  b = b -a;\n  a = a+ b;\n  b = a-b;\n  console.log('After swap Output: ','a: ', a, 'b: ', b);  \n\/\/ After swap Output:  a:  3 b:  2\n}\n\nswapNumb(2, 3);\n<\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Solution &#8211; 2<\/h3>\n\n\n\n<pre class=\"wp-block-code\"><code>function swapNumb(a, b){\n  console.log('Before swap Output: ','a: ', a, 'b: ', b);\n\n  \/\/ Before swap Output:  a:  2 b:  3\n  a = a ^ b;\n  b = a ^ b;\n  a = a ^ b;\n  console.log('After swap Output: ','a: ', a, 'b: ', b); \n \/\/ After swap Output:  a:  3 b:  2\n}\nswapNumb(2, 3);<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full is-style-default\"><img loading=\"lazy\" decoding=\"async\" width=\"1792\" height=\"1024\" data-attachment-id=\"136438\" data-permalink=\"https:\/\/www.sushilkumar.ind.in\/blog\/js-interview-algorithm-question-answers\/attachment\/javascript-algorithm\/\" data-orig-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm.png\" data-orig-size=\"1792,1024\" data-comments-opened=\"1\" data-image-meta=\"{&quot;aperture&quot;:&quot;0&quot;,&quot;credit&quot;:&quot;&quot;,&quot;camera&quot;:&quot;&quot;,&quot;caption&quot;:&quot;&quot;,&quot;created_timestamp&quot;:&quot;0&quot;,&quot;copyright&quot;:&quot;&quot;,&quot;focal_length&quot;:&quot;0&quot;,&quot;iso&quot;:&quot;0&quot;,&quot;shutter_speed&quot;:&quot;0&quot;,&quot;title&quot;:&quot;&quot;,&quot;orientation&quot;:&quot;0&quot;}\" data-image-title=\"javascript-algorithm\" data-image-description=\"\" data-image-caption=\"\" data-medium-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-300x171.png\" data-large-file=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-1024x585.png\" tabindex=\"0\" role=\"button\" src=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm.png\" alt=\"JavaScript Algorithm.\" class=\"wp-image-136438\" srcset=\"https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm.png 1792w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-300x171.png 300w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-1024x585.png 1024w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-768x439.png 768w, https:\/\/www.sushilkumar.ind.in\/blog\/wp-content\/uploads\/2024\/08\/javascript-algorithm-1536x878.png 1536w\" sizes=\"(max-width: 1792px) 100vw, 1792px\" \/><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Find a Duplicate Number in an Array<\/strong><\/h3>\n\n\n\n<p>To identify a duplicate number in an array, you can track the numbers you encounter using a data structure like a set. As you iterate through the array, check if each number is already in the set. If it is, that\u2019s a duplicate; otherwise, add the number to the set.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find the Missing Number in a Given Integer Array<\/h3>\n\n\n\n<p>If you have an array containing numbers from <strong>1<\/strong> to <strong>n<\/strong> with one number missing, you can calculate the expected sum of all numbers from <strong>1<\/strong> to <strong>n<\/strong> using the sum formula for an arithmetic series. Then subtract the sum of the numbers present in the array from this expected sum to find the missing number.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find the Largest and Smallest Number in an Unsorted Array<\/h3>\n\n\n\n<p>To find the largest and smallest numbers, iterate through the array and compare each number to the current known largest and smallest values. Update these values as needed during the iteration.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Return an Array Showing the Cumulative Sum at Each Index<\/h3>\n\n\n\n<p>Create a new array where each element at index <strong>i<\/strong> represents the sum of all elements in the original array up to index <strong>i<\/strong>. You can achieve this by keeping a running total of the sum as you iterate through the original array.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find All Duplicate Numbers in an Array with Multiple Duplicates<\/h3>\n\n\n\n<p>Use a frequency counter to track how many times each number appears in the array. After counting, collect all numbers that appear more than once into a list of duplicates.<br><\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Remove All Duplicates from an Array of Integers<\/h3>\n\n\n\n<p>Use a set to automatically handle uniqueness. By adding each number to the set, duplicates are inherently removed because sets do not allow duplicate entries. Convert the set back to an array to get the result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Find All Pairs in an Array Whose Sum Equals a Given Number<\/h3>\n\n\n\n<p>Use a set to keep track of the numbers you have seen so far. For each number in the array, calculate its complement (the number needed to reach the target sum) and check if this complement is already in the set. If it is, you\u2019ve found a pair<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Replace One Character with Another<\/h3>\n\n\n\n<p>To replace characters, iterate through the string and replace occurrences of a specific character with another character. This can be efficiently done using string manipulation functions available in many programming languages.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/reverse\" title=\"Reverse a String\">Reverse a String<\/a><\/h3>\n\n\n\n<p>To reverse a string, you can convert the string into an array of characters, reverse the order of the characters, and then join the characters back into a string.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Remove All Even Integers from an Array<\/h3>\n\n\n\n<p>Filter out even numbers from the array by checking each number and retaining only those that are odd<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Check and Verify a Word as a Palindrome<\/h3>\n\n\n\n<p>A palindrome reads the same forwards and backwards. To check if a word is a palindrome, compare the string to its reverse. Ignore case and non-alphanumeric characters for a more robust solution<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><a href=\"https:\/\/www.sushilkumar.ind.in\/blog\/javascript\/top-javascript-interview-questions\/\" title=\"Top Javascript Interview Questions and answer\">Find the Factorial of a Number<\/a><\/h3>\n\n\n\n<p>The factorial of a number is the product of all positive integers up to that number. It can be computed iteratively by multiplying the numbers from <strong>1<\/strong> to <strong>n<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. Find a duplicate number in an array of integers in JavaScript. 2. Find the missing number in a given integer Array in JavaScript. 3. Find the largest and smallest number in an unsorted array of integers in JavaScript. 4. Return an array showing the cumulative sum at each index of an array of integers &hellip;<\/p>\n","protected":false},"author":1,"featured_media":136438,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"_acf_changed":false,"jetpack_post_was_ever_published":false,"footnotes":""},"class_list":["post-135897","page","type-page","status-publish","has-post-thumbnail",""],"acf":[],"aioseo_notices":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/P99pkJ-zlT","_links":{"self":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/pages\/135897"}],"collection":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/comments?post=135897"}],"version-history":[{"count":24,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/pages\/135897\/revisions"}],"predecessor-version":[{"id":136464,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/pages\/135897\/revisions\/136464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media\/136438"}],"wp:attachment":[{"href":"https:\/\/www.sushilkumar.ind.in\/blog\/wp-json\/wp\/v2\/media?parent=135897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}