vartotalFruit=function (fruits){letmap=newMap();letmax=0;lettempArray=[];letend=0;letarray=fruits;letk=2;// as total fruits is two you can have at a timewhile (array.length>end){constnextChar=array[end];if (map.size<k&&!map.has(nextChar)){map.set(nextChar,1);end++;tempArray.push(nextChar);}elseif (map.size<=k&&map.has(nextChar)){map.set(nextChar,map.get(nextChar)+1);end++;tempArray.push(nextChar);}elseif (map.size===k&&!map.has(nextChar)){while (map.size===k){// save the currentif (tempArray.length>max){max=tempArray.length;}letstartValue=tempArray.shift();map.set(startValue,map.get(startValue)-1);if (map.get(startValue)===0){map.delete(startValue);}}}}returnmax>tempArray.length?max:tempArray.length;};console.log(totalFruit([1,1,1,1,1,1,1,3,2,1]));console.log(totalFruit([1,2,1],2));console.log(totalFruit([1,2,3,2,2]));console.log(totalFruit([0,1,2,2]));