lodash: Get duplicate values from an array
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
.
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.