0

I have an array with millions of tuple elements like:

var arr: ArrayBuffer[(String, String)] = ArrayBuffer[(String, String)]()
arr += (("Hamburg", "Street1"))
arr += (("Hamburg", "Street2"))
arr += (("Hamburg", "Street1")) // duplicate - remove
arr += (("Berlin",  "StreetA"))
arr += (("Berlin",  "StreetZ"))
arr += (("Berlin",  "StreetZ")) // duplicate - remove
arr += (("Berlin",  "StreetA")) // duplicate - remove

I would now like to have those duplicates within that array removed, where City AND Street are equal. Something like:

arr.distinctBy(_._1&_._2) // doesn't work just for illustration

Is there a simple solution to it, how this can be done to get an output like:

(("Hamburg", "Street1"))
(("Hamburg", "Street2"))
(("Berlin",  "StreetA"))
(("Berlin",  "StreetZ"))
1
  • 2
    You may see if you could have saved all the mutability and array buffer from the beginning and just have created a Set right away. Commented Feb 23, 2022 at 20:41

2 Answers 2

3

Since equals and hashCode are overridden for tuples you can use distinct which is effectively is distinctBy(identity):

val result = arr.distinct
Sign up to request clarification or add additional context in comments.

Comments

3

Calling arr.toSet on your array will do what you require:

arr.toSet 
res34: Set[(String, String)] = Set(
  ("Hamburg", "Street1"),
  ("Hamburg", "Street2"),
  ("Berlin", "StreetA"),
  ("Berlin", "StreetZ")
)

Tuples are case classes, so are provided with equals and hashCode methods to support comparisons.

If your use case is to ensure a collection contains no duplicates, you should generally use a set. This allows other readers of your code to infer that there are no duplicates in the collection.

2 Comments

Might be worth explaining why, meaning how equality is defined for tuples.
Thanks for the recommendation @GaëlJ, I've done as you suggested.

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.