0

The objective is to iterate through the outer loop and then through the inner loop. After that we need to filter the parameter which is passed as 'elem'. The new Array (newArr) should return an array without the 'elem' element.

function filteredArray(arr, elem) {
  let newArr = [];
  
for(var i = 0; i<= arr.length; i ++){
  for(var j = 0; j<=arr[i].length ; j++){

      if(arr.indexOf(elem)!= -1){
        newArr.push(arr[i]);
      }
  }
}
  
  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)); 

What is wrong with this logic ?

2
  • 1
    Why not use filter? Also you seem to be checking if an individual number exists, but the array has arrays not numbers, so this would never pass. Commented Jul 11, 2020 at 5:13
  • 2
    Per @NickMcCurdy comment, Geo Mukkath it is not clear whether you want a flat array or multi-dimensioned array as the return array. Commented Jul 11, 2020 at 5:20

4 Answers 4

2

If you want multidimensional array as a result

function filteredArray(arr, elem) {
    let newArr = [];
  
    for (var i = 0; i < arr.length; i++) {
      let subArray=[];  
      for (var j = 0; j < arr[i].length; j++) {
        if (arr[i][j] !==elem) {
          subArray.push(arr[i][j]);
        }
      }
      newArr.push(subArray)
    }
  
    return newArr;
  }
  
  console.log((filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)));

And if you want flat array as a result

    function filteredArray(arr, elem) {
      let newArr = [];

      for (var i = 0; i < arr.length; i++) {
        for (var j = 0; j < arr[i].length; j++) {
          if (arr[i][j] !==elem) {
            newArr.push(arr[i][j]);
          }
        }
      }
    
      return newArr;
    }
    
    console.log((filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3)));

Sign up to request clarification or add additional context in comments.

Comments

1

Without using filter method, But if you can use map and reduce will be simplified and can avoid handling with indexes.

const filteredArray = (arr, elem) =>
  arr.map((data) =>
    data.reduce((acc, cur) => (cur !== elem && acc.push(cur), acc), [])
  );

console.log(
  filteredArray(
    [
      [3, 2, 3],
      [1, 6, 3],
      [3, 13, 26],
      [19, 3, 9],
    ],
    3
  )
);

If you need flat array, just change map to flatMap in above code.

const filteredFlatArray = (arr, elem) =>
  arr.flatMap((data) =>
    data.reduce((acc, cur) => (cur !== elem && acc.push(cur), acc), [])
  );

console.log(
  filteredFlatArray(
    [
      [3, 2, 3],
      [1, 6, 3],
      [3, 13, 26],
      [19, 3, 9],
    ],
    3
  )
);

Comments

0

Corrections

  • i <= arr.length => i < arr.length
  • arr.indexOf(elem) => arr[i].indexOf(elem)

function filteredArray(arr, elem) {
  let newArr = [];

  for (var i = 0; i < arr.length; i++) {
    for (var j = 0; j < arr[i].length; j++) {

      if (arr[i].indexOf(elem) != -1) {
        newArr.push(arr[i]);
      }
    }
  }

  return newArr;
}

console.log(JSON.stringify(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 3, 9]], 3))); 

Comments

0

What is wrong with this logic ?

1-You need to declare empty sub-arrays before accessing them.

newArr[i] = [];

2-You want to push the full array(I assume to save time), if elem is NOT found, correct condition or put in else.

newArr.push(arr[i]); but you should use this 
newArr[i] = arr[i]; because i created new empty sub arrays.

3-You need to actually use j to go throught subarray.

newArr[i].push(arr[i][j]);

4-It was already answered, but you need to check you dont overshoot out of array.

i < arr.length j < arr[i].length

5-You are missing extreme cases.

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 5, 9], [3, 3, 3]], 3));

function filteredArray(arr, elem) {
  const newArr = [];
  let skip = 0;

  for (var i = 0; i < arr.length; i++) {
    newArr[i] = [];
    skip = arr[i].indexOf(elem);
    for (var j = 0; j < arr[i].length; j++) {
      if (skip !== -1) {
        if (arr[i][j] !== elem) {
          newArr[i].push(arr[i][j]);
        }
      } else {
        newArr[i] = arr[i];
        break;
      }
    }
  }

  return newArr;
}

console.log(filteredArray([[3, 2, 3], [1, 6, 3], [3, 13, 26], [19, 5, 9], [3, 3, 3]], 3)); 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.