kyrolos magdy
kyrolos magdy

Reputation: 393

Locating a duplicated item in an array and get the length between them

I got an array like so

const array = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1]

and what I want is to just locate these ones and get the number of zeroes between these ones, and then return the biggest length of them so in this case it will return 5 and if the case was like so

const array = [1,0,0,0,0,0,1] 

then the output should be 5 either and if there wasn't but one one in the array like so

const array = [1,0,0,0]

I should get err

I have tried to locate the first one using .findIndex() like so

const firstOneIndex = intoArray.findIndex(el => el = 1);

Upvotes: 1

Views: 44

Answers (2)

DerDjamel
DerDjamel

Reputation: 114

here is my attempt on it ,I have 2 solution one is the iteration solution and the other is using recursion

Solution #1

var array = [0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]; // the original array
var index   = array.indexOf(1);                   // store the index of the first 1
var diff = 0;                                     // this will hold the max diff between the ones

// if exist at least one 1 and there is more than one 1 
if( index != -1 || index != array.lastIndexOf(1)){
  // if there is more than one 1 then store the index of the 2nd 1
  var index2  = array.indexOf(1, index);
  // start looping in the array and search for the mac diff by calculating the diff between the first 2 ones and do it again
  while( index < array.length && index2 != -1 ){
    // see if the last diff if bigger than the store the max of them in diff
    diff = ((index2 - index) - 1) > diff ? ((index2 - index) - 1) : diff;
    // adjust the indexs to continue doing the same 
    index = index2;
    index2 = array.indexOf(1, index2 + 1);
  }

  console.log(diff = diff > 0 ? diff : 'error');
}

Solution #2 (recursion)

var array = [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 1]; // the original array


function howManyZeros(array){
  var diff = 0;
  console.log(array);
  if( array.length > 1 ){
    let index1 = array.indexOf(1);
    let index2 = array.indexOf(1, index1 + 1);
    if( index1 != -1 && index2 != -1){
      diff = (index2 - index1) - 1;
      return diff < howManyZeros(array.slice(index2)) ? howManyZeros(array.slice(index2)) : diff;
    }
    return diff;
  }
  return diff;
}

console.log(howManyZeros(array));

I hope this helps.

Upvotes: 0

Zze
Zze

Reputation: 18805

const arr = [1, 0, 1, 1, 0, 0, 1];

function getDiff(array) {
  // identify the 1's in the array
  // strip the array down to a list of only the ones with values.
  var mapped = array.map((v, i) => {
    if (v == 1) return i;
  }).filter(v => v != undefined);

  var max = 0
  var start;
  var end;
  // identify the largest gap between 1's
  for (var ind in mapped) {
    var gap = mapped[ind] - mapped[ind - 1];
    // store largest gap start and stop indexs
    if (gap > max) {
      start = ind - 1;
      end = ind;
      max = gap;
    }
  }
  
  // we do mapped[end] +1 because we want to include the last 1 in the splice, not exclude it.
  var splice = array.splice(mapped[start], mapped[end] + 1);

  return splice;
}

console.log(getDiff(arr));

Upvotes: 1

Related Questions