Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learn which values of an array are non-unique? #918

Closed
MeoMix opened this issue Feb 3, 2015 · 2 comments

Comments

@MeoMix
Copy link

commented Feb 3, 2015

Hey,

I have an array that is: [1,1,2,3,6,7,7,9] and I would like to transform this into [1,7]

I was a bit surprised that there doesn't seem to be a clear path to achieving this. Am I missing something really obvious? _.uniq is close, but doesn't tell me which ones it removed. I'd think I should be able to diff _.uniq and original, but that'll remove all of the elements...

Right now I'm rocking something ugly:

var nonUnique = _.chain([1, 1, 2, 3, 6, 7, 7, 9])
  .groupBy(function (val) { return val; })
  .filter(function (arr) { return arr.length > 1; })
  .map(function (arr) { return arr[0]; })
  .value();

or maybe

var foo = [1, 1, 2, 3, 6, 7, 7, 9];
var nonUnique = _.transform(_.groupBy(foo), function(data, value, key) {
  if (value.length > 1) {
    data.push(parseInt(key));
  }
}, []);
@jdalton

This comment has been minimized.

Copy link
Member

commented Feb 3, 2015

Hi @MeoMix. You can do:

_.transform(_.countBy(array), function(result, count, value) {
  if (count > 1) result.push(value);
}, []);

@jdalton jdalton closed this Feb 3, 2015

@jdalton jdalton added the question label Feb 3, 2015

@lock

This comment has been minimized.

Copy link

commented Jan 20, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot locked as resolved and limited conversation to collaborators Jan 20, 2019

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
2 participants
You can’t perform that action at this time.