3

I have a list of object PersonInfo, if certain fields in the PersonInfo object are same with another PersonInfo object, i'll say these two objects are duplicated. example:

case class PersonInfo(
    firstName: Instant,
    lastName: Instant,
    ssn: String,
    email: String
)

if two PersonInfo objects have same 'ssn', they are duplicated record. my list looks like this:

val list = List(pi1, pi2, pi3)
pi1 is: PersonInfo("foo", "foo", "123-456-789", "[email protected]")
pi2 is: PersonInfo("bar", "bar", "456-123-789", "[email protected]")
pi3 is: PersonInfo("gee", "gee", "123-456-789", "[email protected])

how can i filter the list to only return list of (pi1 and pi3) since pi1 and pi3 are duplicated:

list.filter(f => pi1.ssn == pi3.ssn) => ???

and I expect it to return List(pi1, pi2)

3
  • 4
    You'll probably find the answer here: stackoverflow.com/questions/3912753/… Commented May 15, 2019 at 15:43
  • 2
    You can override the equals method on your case class and use distinct. Commented May 15, 2019 at 15:46
  • I think this part of the question only return list of (pi1 and pi3) conflicts with expect it to return List(pi1, pi2) Commented May 15, 2019 at 16:27

2 Answers 2

3

Group them, keep only the duplicates, return as List.

list.groupBy(_.ssn).values.filter(_.length > 1).flatten.toList
Sign up to request clarification or add additional context in comments.

Comments

2

I would use groupBy + flatMap:

val pi1 = PersonInfo("foo", "foo", "123-456-789", "[email protected]")
val pi2 = PersonInfo("bar", "bar", "456-123-789", "[email protected]")
val pi3 = PersonInfo("gee", "gee", "123-456-789", "[email protected]")

val list = List(pi1, pi2, pi3)

val onlyDuplicates = list
    .groupBy(_.ssn)
    .flatMap{
      case (_, v) if v.length > 1 => v //take only elements that have 1+ occurence
      case _ => Nil
    }

println(onlyDuplicates) // List(pi1, pi3)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.