lodash: Get duplicate values from an array
lodash: Get duplicate values from an array
Say I have an array like this: [1, 1, 2, 2, 3]
[1, 1, 2, 2, 3]
I want to get the duplicates which are in this case: [1, 2]
[1, 2]
Does lodash support this? I want to do it in the shortest way possible.
Possible duplicate of Using lodash to check whether an array has duplicate values
– Gajus
Feb 29 '16 at 16:19
8 Answers
8
You can use this:
_.filter(arr, (val, i, iteratee) => _.includes(iteratee, val, i + 1));
Note that if a number appears more than two times in your array you can always use _.uniq.
_.uniq
This is awesome!
– Adam Boduch
Jul 28 '15 at 16:57
One-liner, with some ES2015 sugar:
const duplicates = _.filter(array, (value, index, iteratee) => _.includes(iteratee, value, index + 1))– Pier-Luc Gendreau
May 19 '16 at 17:46
const duplicates = _.filter(array, (value, index, iteratee) => _.includes(iteratee, value, index + 1))
It works with objects and more complex structures if you use
_.find instead of _.includes _.filter(array, (value, index, iteratee) => return _.find(iteratee, value, index + 1) )– maraujop
Apr 3 '18 at 14:50
_.find
_.includes
_.filter(array, (value, index, iteratee) => return _.find(iteratee, value, index + 1) )
Nice one! Took me a moment to figure out what this actually does, but now I understand Lodash a bit better – sweet!
– panepeter
Dec 14 '18 at 14:27
Another way is to group by unique items, and return the group keys that have more than 1 item
_([1, 1, 2, 2, 3]).groupBy().pickBy(x => x.length > 1).keys().value()
var array = [1, 1, 2, 2, 3];
var groupped = _.groupBy(array, function (n) return n);
var result = _.uniq(_.flatten(_.filter(groupped, function (n) return n.length > 1)));
This works for unsorted arrays as well.
This seems to be significantly faster for larger arrays than the accepted answer is. Nice work.
– James Pizzurro
Mar 1 '17 at 17:39
Another way, but using filters and ecmaScript 2015 (ES6)
var array = [1, 1, 2, 2, 3];
_.filter(array, v =>
_.filter(array, v1 => v1 === v).length > 1);
//→ [1, 1, 2, 2]
I like, very simple! The trick is that it identifies if more than one exists on the sub-filter.
– justinpage
Sep 7 '16 at 23:17
How about using countBy() followed by reduce()?
countBy()
reduce()
const items = [1,1,2,3,3,3,4,5,6,7,7];
const dup = _(items)
.countBy()
.reduce((acc, val, key) => val > 1 ? acc.concat(key) : acc, )
.map(_.toNumber)
console.log(dup);
// [1, 3, 7]
http://jsbin.com/panama/edit?js,console
Well you can use this piece of code which is much faster as it has a complexity of O(n) and this doesn't use Lodash.
[1, 1, 2, 2, 3]
.reduce((agg,col) =>
agg.filter[col] = agg.filter[col]? agg.dup.push(col): 2;
return agg
,
filter:,dup:)
.dup;
//result:[1,2]
here is mine, es6-like, deps-free, answer. with filter instead of reducer
// this checks if elements of one list contains elements of second list
// example code
[0,1,2,3,8,9].filter(item => [3,4,5,6,7].indexOf(item) > -1)
// function
const contains = (listA, listB) => listA.filter(item => listB.indexOf(item) > -1)
contains([0,1,2,3], [1,2,3,4]) // => [1, 2, 3]
// only for bool
const hasDuplicates = (listA, listB) => !!contains(listA, listB).length
edit:
hmm my bad is: I've read q as general question but this is strictly for lodash, however my point is - you don't need lodash in here :)
Hope below solution helps you and it will be useful in all conditions
hasDataExist(listObj, key, value): boolean
return _.find(listObj, function(o) return _.get(o, key) == value ) != undefined;
let duplcateIndex = this.service.hasDataExist(this.list, 'xyz', value);
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Is an array already sorted?
– Kiril
Jul 28 '15 at 17:39