GabrielG
GabrielG

Reputation: 21

In Scala what is the most efficient way to remove elements in a list based on being similar to another element?

I have a long list of objects around 300, with each object in the list having this data structure:

case class MyObject(id: String,
                    name: String,
                    colour: String,
                    price: Int
                    height: Int
                    width: Int,
                    desc: String)

I can’t work out what is the best way to go through the list and for each object remove any other object that has the same name, colour, price, height and width. Note that this isn’t a simple dedupe as the ids and desc can be different. The input and output need to remain List[MyObject] and I do not know beforehand which objects are the duplicated ones.

This is my initial solution which works, but not sure its the most efficient way of doing it when it comes to dealing with large list.

def removeDuplicates(originalList: List[MyObject]): List[MyObject] = {

  def loop(remaining: List[MyObject], acc: List[MyObject]): List[MyObject] = {
    remaining match {
      case head :: tail =>
        val listOfDuplicates = tail.filter{ x =>
          x.name == head.name &&
          x.colour == head.colour &&
          x.price == head.price &&
          x.height == head.height &&
          x.width == head.width
        }

        val deDupedTail = tail.filter(!listOfDuplicates.contains(_))

        loop(deDupedTail, acc ::: listOfDuplicates)
      case Nil => acc
    }
  }
  val listOfDuplicateObjects = loop(originalList, List())
  originalList.filter(!listOfDuplicateObjects.contains(_))
}

Upvotes: 2

Views: 255

Answers (1)

amorfis
amorfis

Reputation: 15770

Not sure if it's most efficent, but IMHO it's elegant:

originalList.distinctBy(o => (o.name, o.colour, o.price, o.height, o.width))

Upvotes: 6

Related Questions