JavaScript- Delete all Occurrences in a JS Array Last Updated : 18 Nov, 2024 Summarize Comments Improve Suggest changes Share Like Article Like Report These are the following ways to remove all Occurrences of an element in a JS Array: 1. Using filter() method( Simple and Easy for Long Arrays)The filter() method creates a new array with all elements that passed the test implemented by the given callback function. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } return a.filter( (item) => item !== e); } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 2. Using splice() method(Efficient for Mid range Array)The splice() method changes the contents of an array by removing or replacing existing elements and/or adding new elements in place. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } for (let i = a.length - 1; i >= 0; i--) { if (a[i] === e) { a.splice(i, 1); } } return a; } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 3. Using forEach() method (Efficient for Short Array)The forEach() method will be used to iterate through each element of the array and make changes in it according to the condition passed inside the callback function. JavaScript function fun(a, e) { if (!a.includes(e)) { return -1; } let a1 = []; a.forEach(item => { if (item !== e) { a1.push(item); } }); return a1; } const a = [1, 2, 3, 4, 2, 5, 2]; const res = fun(a, 2); console.log(res); Output[ 1, 3, 4, 5 ] 4. Using reduce method(Efficient For Long Arrays)In this approach we use the reduce method to iterate over each element of the array and accumulate the elements that do not match the element to remove. JavaScript function fun(a, e) { return a.reduce((acc, c) => { if (c !== e) { acc.push(c); } return acc; }, []); } let a = [1, 2, 3, 4, 2, 5, 2]; let e = 2; let a1 = fun(a, e); console.log(a1); Output[ 1, 3, 4, 5 ] Comment More infoAdvertise with us Next Article JavaScript- Delete all Occurrences in a JS Array A anjugaeu01 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-Program Similar Reads JavaScript - Remove all Occurrences of a Character in JS String These are the following ways to remove all occurrence of a character from a given string: 1. Using Regular ExpressionUsing a regular expression, we create a pattern to match all occurrences of a specific character in a string and replace them with an empty string, effectively removing that character 2 min read JavaScript - Access Elements in JS Array These are the following ways to Access Elements in an Array:1. Using Square Bracket NotationWe can access elements in an array by using their index, where the index starts from 0 for the first element. We can access using the bracket notation.JavaScriptconst a = [10, 20, 30, 40, 50]; const v = a[3]; 2 min read Checking for Duplicate Strings in JavaScript Array Checking for duplicate strings in a JavaScript array involves identifying if there are any repeated string values within the array. Given an array of strings a with size N, find all strings that occur more than once. If none is found, return [-1]. Example: Input: N = 4arr = ['apple', 'banana', 'oran 3 min read JavaScript Program to Find the First Non-Repeated Element in an Array Finding the first non-repeated element in an array refers to identifying the initial occurrence of an element that does not occur again elsewhere within the array, indicating uniqueness among the elements.Examples: Input: {-1, 2, -1, 3, 0}Output: 2Explanation: The first number that does not repeat i 5 min read JavaScript Program to Remove Duplicate Elements From a Sorted Array Given a sorted array arr[] of size N, the task is to remove the duplicate elements from the array. Examples: Input: arr[] = {2, 2, 2, 2, 2} Output: arr[] = {2} Explanation: All the elements are 2, So only keep one instance of 2. Input: arr[] = {1, 2, 2, 3, 4, 4, 4, 5, 5} Output: arr[] = {1, 2, 3, 4, 3 min read Like