exception handling

const arr= [9,3,1,8,12];

/**

 * find the missing Numbers without built in method

 * output-->[2,4,5,6,7,10,11]

 * 

 * Approach-->

 * sort the Array [1,3,8,9,12]

 * iterate and check for missingNumber

 * push the missing number in a new Array

*/


const getSortedArr =(array)=>{

  for(let i=array.length;i>0;i--){

    let isSwapped ;

    for(let j=0; j<i-1;j++){

      if(array[j]>array[j+1]){

      [array[j],array[j+1]]= [array[j+1],array[j]]

       isSwapped= true;

      }

    }

    if(!isSwapped){

      break;

    }

  }

  return array

}



/**

 * @params number[]

 * @function findMissingNumber

 * return number[]

*/

const findMissingNumber=(arr)=>{

  const sorted= new Set(getSortedArr(arr));

  const getMaxLength=  [...sorted].pop();

  const missingArray=[];


  for(let i=1; i<=getMaxLength;i++){

    if(!sorted.has(i)){

      missingArray.push(i)

    }

  }

  return missingArray;

}

const res =findMissingNumber(arr)

console.log(res);


Comments

Popular posts from this blog

TO the new

4048