Scala Check if List Contains Duplicates

1. Introduction

In Scala, checking if a list contains duplicates is a common requirement, especially when ensuring data uniqueness is crucial. There are several ways to check for duplicates in a list, each with its own advantages. This blog post will demonstrate how to identify if a list in Scala contains duplicate elements.

2. Program Steps

1. Create a list that may contain duplicates.

2. Apply different methods to check for duplicates, including using a Set and custom logic.

3. Print the results to verify whether duplicates are present.

4. Execute the code to test each method.

3. Code Program

object ListDuplicatesCheckDemo extends App {
  val numbers = List(1, 2, 3, 2, 5)

  // Method 1: Using toSet and comparing lengths
  val hasDuplicates1 = numbers.toSet.size != numbers.size
  println(s"Has duplicates (using toSet)? $hasDuplicates1")

  // Method 2: Using groupBy and finding groups with more than one element
  val hasDuplicates2 = numbers.groupBy(identity).values.exists(_.size > 1)
  println(s"Has duplicates (using groupBy)? $hasDuplicates2")

  // Method 3: Using custom logic with foldLeft
  val hasDuplicates3 = numbers.foldLeft((false, Set[Int]())) {
    case ((result, seen), item) =>
      if (seen.contains(item)) (true, seen)
      else (result, seen + item)
  }._1
  println(s"Has duplicates (using custom logic)? $hasDuplicates3")
}

Output:

Has duplicates (using toSet)? true
Has duplicates (using groupBy)? true
Has duplicates (using custom logic)? true

Explanation:

1. The first method converts the list to a Set (which removes duplicates) and compares its size to the original list's size. If the sizes are different, it means there were duplicates.

2. The second method uses groupBy to group identical elements and then checks if any group has more than one element.

3. The third method involves a foldLeft operation that keeps track of seen elements and checks for duplicates as it iterates through the list.

4. In all cases, the output is true, indicating that the list numbers contains duplicates.


Comments