Find and Remove element from array

medium

By - Aman Pareek

Last Updated - 27/08/2024

Problem Statement

Write a JavaScript function that removes an element from an array at a specified index. The function must validate inputs and handle errors appropriately.

Example 1

Input: array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11] , position = 10

Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Example 2

Input: array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] , position = -4

Output: Throw error => Position must be a valid integer within the array bounds

Solution 1: Using splice() Method

function removeElementUsingSplice(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    arr.splice(position, 1);
    return arr;
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingSplice(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingSplice(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

A hash map (or dictionary) is a great way to solve this problem eff…

Solution 2: Using slice() Method

function removeElementUsingSlice(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    return arr.slice(0, position).concat(arr.slice(position + 1));
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingSlice(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingSlice(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 3: Using filter() Method

function removeElementUsingFilter(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    return arr.filter((_, index) => index !== position);
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFilter(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFilter(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 4: Using reduce() Method

function removeElementUsingReduce(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    return arr.reduce((acc, curr, index) => {
        if (index !== position) acc.push(curr);
        return acc;
    }, []);
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingReduce(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingReduce(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 5: Using for Loop

function removeElementUsingForLoop(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    let result = [];
    for (let i = 0; i < arr.length; i++) {
        if (i !== position) {
            result.push(arr[i]);
        }
    }
    return result;
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingForLoop(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingForLoop(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 6: Using map() Method

function removeElementUsingMap(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    return arr.map((item, index) => (index === position ? undefined : item))
              .filter(item => item !== undefined);
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingMap(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingMap(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 7: Using copyWithin() Method

function removeElementUsingCopyWithin(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    arr.copyWithin(position, position + 1);
    arr.length--; // reduce array length by 1
    return arr;
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingCopyWithin(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingCopyWithin(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 8: Using fill() Method

function removeElementUsingFill(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    arr.fill(undefined, position, position + 1);
    return arr.filter(item => item !== undefined);
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFill(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFill(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 9: Using findIndex() Method with splice()

function removeElementUsingFindIndex(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    let index = arr.findIndex((_, i) => i === position);
    if (index !== -1) {
        arr.splice(index, 1);
    }
    return arr;
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingFindIndex(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingFindIndex(array2,position2);  //Throw error => Position must be a valid integer within the array bounds 

Solution 10: Using Array.from() and filter()

function removeElementUsingArrayFrom(arr, position) {
    if (!Array.isArray(arr)) {
        throw new TypeError('First argument must be an array');
    }
    if (!Number.isInteger(position) || position < 0 || position >= arr.length) {
        throw new RangeError('Position must be a valid integer within the array bounds');
    }
    return Array.from(arr).filter((_, index) => index !== position);
} 

const array1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11];
const position1 = 10;
removeElementUsingArrayFrom(array1,position1);  //output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

const array2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
const position2 = -4;
removeElementUsingArrayFrom(array2,position2);  //Throw error => Position must be a valid integer within the array bounds