How to remove the single instance from an array in javascript?

Viewed 1491

I need to find max number then remove it from array.(only single instance)

let array is

a=[91,65,91,88,26]

I am finding max using Math.max()

k=Math.max(...a)

Now using filter() it

a=a.filter(e => e!=k);

But its filtering both the instances of max number.

How to resolve it?

4 Answers

Here are two simple ways to do it:

First way using splice()

a=[91,65,91,88,26];
a = a.sort();
a.splice(-1, 1);
console.log(a);

// Array [ 26, 65, 88, 91 ]

Second way using pop()

a=[91,65,91,88,26];
a = a.sort();
a.pop();
console.log(a);

// Array [ 26, 65, 88, 91 ]

As you have discovered, .filter() iterates the entire array, testing each value against your filter function. That is not the task you have articulated.

The key to removing a single element is .splice(). Your task is simply to tell splice which item(s) to remove:

a.splice(a.indexOf( k ), 1);

Alternatively, you could remove the last index with:

a.splice(a.lastIndexOf( k ), 1);

In "human", arr.splice(k, n) reads "Starting at index k, remove the next n elements."

Of course, if you don't mind the overhead of sorting (or changing the item order!), then you can do something like:

a.sort().pop();

Which will sort the array, and then remove the final element -- which is also the largest.

You could find the index of the element you want to remove and remove just that. Not writing Sort assuming you'd want to retain the order of the array.

So that would require using findIndex.

k=Math.max(...a)
i=a.findIndex(el => el === k)
newArray = [...a.slice(0, i), ...a.slice(i+1)]

Using array slice in the manner I've done also makes sure we don't change the initial array.
Although I would not advise using this for very large arrays.
You'd get best performance by writing your own function that does all of this.

Here is a functional version of the task that doesn't mutate your original array. Please see the inline comments for an explanation.

const a = [91,65,91,88,26]

// return the max out of two numbers
const max = (x, y) => x > y ? x : y

const removeMax = a => {
  // find the largest value in the array with reduce
  const largest = a.reduce(max, 0)
  // get the first index of the largest number
  const index = a.indexOf(largest)
  // return a new array without the largest number
  return [
    ...a.slice(0, index),
    ...a.slice(index + 1)
  ]
}

console.log('before', a)

console.log('after', removeMax(a))
<script src="https://codepen.io/synthet1c/pen/KyQQmL.js"></script>

Related