Comments

Paolo Micheletti22 days ago

For those who are saying that filter isn't working: const solve = (intArray) => { const result = intArray.filter(el => intArray.indexOf(el) === el) return parseInt(result[0]); }

Berkshire revolutionary South Carolina2 months ago

const solve = (intArray) => { let matchingInteger = ""; for (let index of intArray) { if (index == intArray.indexOf(index)) { return (matchingInteger = index); break; } } };

integrated Prairie SDD2 months ago

am new here and am comfuse already

Juliette Merle4 months ago

My code, python class Solution: def solve(self, int_array): for i in range(len(int_array)): if i == int_array[i]: return int_array[i]

Nathan Dalton4 months ago

const solve = (intArray) => { function equalToIndex(num, index) { return num === index; } return intArray.findIndex(equalToIndex); };

newyorkcityjose7 months ago

const solve = (intArray) => { return intArray.filter( (x, i) => x[i] === i) };

Jinyan Shen6 months ago

This solution doesn't seem to be correct. 'x' is already the element in index 'i', to make it work I think it should be 'return intArray.filter( (x, i) => x === i)'

That said, using '.filter' is not giving the correct answer because '.filter' returns an array, while the problem suggests the answer should be a single value, not an array...

Jinyan Shen6 months ago

But the above solution got passed (because I came up with this solution in the first place as well), so there might be some issues with the solution testing, I am afraid...

client-server port Incredible7 months ago

const solve = (intArray) => { for(let index in intArray) { if(intArray[index] == index) { return intArray[index]; } } };

Perry Hunchak8 months ago

const solve = (intArray) => { for(let i =0;i<intArray.length;i++) { if (intArray[i] === i) { return i; } } };

Arvind Pal8 months ago

Hi anyone please let me know why case one fails while second one not? (JUST INTERCHANGED the comparision)

Case 1

const solve = (intArray) =>  intArray.find((el, i) => i===el);

Case 2.

const solve = (intArray) =>  intArray.find((el, i) => el===i);

Dustin Möllera year ago

Bit more basic:

  let result = '';
  for (var i = 0; i < intArray.length; i++) {
    if (i === intArray[i]) {
      result = intArray[i];
      break;
    }
  }
  return result;

Narek Musakhanyana year ago

const solve = (intArray) => {
  return intArray.find((item, index) => index === item)
};

Mohamed El Ghannaya year ago

good one, here is my solution:

const solve = (intArray) => {
  const maps = intArray.filter((el, i) => el === i);
  return maps[0];
};

Daniel Cortes2 years ago

Is this site flawed?

This solution returns the correct answer on my IDE:

const solve = (intArray) => {
	const result = intArray.filter((n, i) => {
  	if (n === i) {
    	return n;
    }
  });
  
  return parseInt(result);
}

Tobias Fried2 years ago

Array.prototype.filter() returns a new array of the items that satisfy the filter condition. You should only be looking for the first match, not all of them. Also, not sure how parseInt() deals with parsing an array...

Daniel Cortes2 years ago

Hey guys, this solution I wrote works inside a REPL.IT:

const solve = (intArray) => {
  const result = intArray.forEach(function(num, index){
    if (num === index) {
      console.log(num);
     }
  });
  return result;
};

but does not work here. Any suggestions on what I need to change to get it passing here?

Tobias Fried2 years ago

Array.prototype.forEach() does not return anything (well, it returns undefined) and is meant for DOING something FOR each element. See [MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach). You could use one of the other array methods like filter() or map() to get something back, but I found here the best solution with the least overhead that finishes as soon as it finds a match is a good old for loop:

const solve = (intArray) => {
  for(let i = 0; i < intArray.length; i++) {
    if (intArray[i] === i) return i;
  }
};

Justin Kim2 years ago

const solve = (intArray) => intArray.reverse().reduce((acc,n,i, arr) => n === (arr.length - i - 1) ? n : acc, 0)

Michael Morgan2 years ago

const solve = (intArray) => { return intArray.find((num, index) => num === index); };

DennisLoska2 years ago

const solve = (intArray) => {
  return intArray.find(int => int === intArray.indexOf(int))
};

Leonardo Studart2 years ago

const solve = (intArray) => { return intArray.find(el => el === intArray.findIndex(item => item === el)); };

Tobias Fried2 years ago

Two find calls could be quite expensive!