How to Remove Duplicate Elements from an Array using Lodash ?
Last Updated :
14 May, 2024
Removing duplicate elements from an array is necessary for data integrity and efficient processing. The approaches implemented and explained below will use the Lodash to remove duplicate elements from an array.
Run the below command before running the code on your system:
npm i lodash
Using uniq method
In this approach, we will use Lodash's uniq function, which returns a new array with unique elements from the input array, removing duplicates while maintaining the order of elements.
Syntax:
_.uniq(array)
Example: The below example uses the uniq function to remove duplicate elements from an array using Lodash.
JavaScript
const _ = require('lodash');
let arr =
["apple", "mango", "apple",
"orange", "mango", "mango"];
let res = _.uniq(arr);
console.log(res);
Output:
[ 'apple', 'mango', 'orange' ]
Using groupBy and map methods
In this approach, we will use the Lodash's groupBy function to group the elements of the array based on their values, and then using the Lodash's map to extract the unique keys (values) from the grouped object, removing duplicates while preserving the order of elements.
Syntax:
let grouped = _.groupBy(array),
let uniqueArr = _.map(grouped, (value, key) => key);
Example: The below example uses groupBy and map functions to remove duplicate elements from an array using Lodash.
JavaScript
const _ = require('lodash');
let arr =
["apple", "mango", "apple",
"orange", "mango", "mango"];
let grouped = _.groupBy(arr);
let res = _.map(grouped,
(value, key) => key);
console.log(res);
Output:
[ 'apple', 'mango', 'orange' ]
Using xor function
In this approach, we are using the xor function of Lodash with an empty array as the second argument to create a new array containing unique elements from the input array arr, removing duplicates.
Syntax:
_.xor([arrays])
Example: The below example uses the xor function to remove duplicate elements from an array using Lodash.
JavaScript
const _ = require('lodash');
let arr =
["apple", "mango", "apple",
"orange", "mango", "mango"];
let res = _.xor(arr, []);
console.log(res);
Output:
[ 'apple', 'mango', 'orange' ]
Using uniqBy Method
The uniqBy method in Lodash removes duplicate elements from an array based on a specified criterion. It allows us to define a function that extracts a unique identifier from each element, enabling customized handling of duplicate removal.
Example: In this example, we use the uniqBy method to remove duplicate elements from an array based on a custom criterion defined by a function. The function extracts the length of each string element as the unique identifier.
JavaScript
const _ = require('lodash');
let arr = ["apple", "mango", "apple", "orange", "mango", "mango"];
let res = _.uniqBy(arr, (element) => element.length);
console.log(res);
Output:
[ 'apple', 'mango', 'orange' ]
Similar Reads
How to Remove Duplicate Elements from Array in Ruby? This article focuses on discussing how to remove duplicate elements from an array in Ruby. In Ruby, an array is a fundamental data structure that stores a collection of elements in a specific order. Various methods and approaches can be used to remove duplicate elements from an array. Removing Dupli
2 min read
How to Remove duplicate elements from array in JavaScript ? In this article, we will discuss the methods to remove duplicate elements from a Javascript array. There are various methods to remove duplicates in the array. These are the following ways: Table of Content Using filter() MethodUsing set() MethodUsing forEach() MethodUsing reduce() MethodUsing index
4 min read
Remove duplicate elements from an array using AngularJS We have given an array and the task is to remove/delete the duplicates from the array using AngularJS.Approach: The approach is to use the filter() method and inside the method, the elements that don't repeat themselves will be returned and the duplicates will be returned only once.Hence, a unique a
2 min read
How to remove duplicate values from array using PHP? In this article, we will discuss removing duplicate elements from an array in PHP. We can get the unique elements by using array_unique() function. This function will remove the duplicate values from the array.Syntax:array array_unique($array, $sort_flags);Note: The keys of the array are preserved i
4 min read
Remove Duplicate Elements from TypeScript Array TypeScript array is a single variable that is used to store the elements or a group of values of the same type. An array can contain multiple occurrences or duplicates of an element that can be removed using the below-discussed methods.Below are the approaches used to remove Duplicate Elements from
4 min read
How to Remove Duplicates from an Array of Objects using TypeScript ? We are given an array of objects and we have to check if there are duplicate objects present in the array and remove the duplicate occurrences from it. Otherwise, return the array as it is. Examples of Removing Duplicates from an Array of Objects using TypeScript Table of Content Using filter() and
4 min read
Remove Duplicate Elements from JavaScript Array To Remove the elements from an array we can use the JavaScript set method. Removing duplicate elements requires checking if the element is present more than one time in the array. 1. Using JavaScript Set() - Mostly UsedThe JavaScript Set() method creates an object containing only unique values. To r
3 min read
Golang Program that Removes Duplicate Elements From the Array Arrays in Golang or Go programming language is much similar to other programming languages. In the program, sometimes we need to store a collection of data of the same type, like a list of student marks. Such type of collection is stored in a program using an Array. An array is a fixed-length sequen
4 min read
How To Get Duplicate Object Fields From Collection Using Lodash? Finding duplicate object fields in a collection is a common problem when working with large datasets. Lodash provides several utilities that can help identify these duplicates easily by comparing properties within arrays of objects. In this article, weâll explore different approaches to g duplicate
3 min read
How to Remove an Element from a list in Lodash? To remove an element from a list, we employ Lodash functions such as without, pull, or reject. These methods enable us to filter out elements from the list based on specific conditions, effectively excluding those that match the criteria. Below are the approaches to remove an element from a list in
2 min read