Four Ways to Remove Duplicates From an Array in Kotlin

Introduction

There are a few ways to remove duplicates from an array in kotlin:

  1. Using the toSet() function
  2. Using the toHashSet() function
  3. Using the toMutableSet() function
  4. Using the distinct() function

To understand the usage of each function let us consider the following example:

You have a data class called Node:

data class Node(val id: Int, val fullyQualifiedName: String)

and, an array of Nodes:

val nodes = arrayOf(
	Node(1, "dev.droidchef.usecase.GetAllNodes"),
	Node(2, "dev.droidchef.usecase.DeleteAllNodes"),
	Node(3, "dev.droidchef.usecase.SetAllNodes"),
	Node(4, "dev.droidchef.usecase.GetAllNodes"),
	Node(5, "dev.droidchef.usecase.WipeAllNodes")
)

Using the toSet() function

This function will create a new Set (which is a data structure that doesn't allow duplicate entries) from the given array. This is the simplest and the best way to remove duplicates from an array in kotlin.

Points To Remember

  • Return type of toSet() is a Set<T>
  • Internally it calls toCollection()
  • It guarantees the preservation of the iteration order of the elements.

Code Sample

data class Node(val id: Int, val fullyQualifiedName: String)

val nodes = arrayOf(
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(2, "dev.droidchef.usecase.DeleteAllNodes"),
    Node(3, "dev.droidchef.usecase.SetAllNodes"),
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(4, "dev.droidchef.usecase.WipeAllNodes")
)

val deDupedNodes = nodes.toSet()
deDupedNodes.forEach {
    println(it)
}

Code Output

Node(id=1, fullyQualifiedName=dev.droidchef.usecase.GetAllNodes)
Node(id=2, fullyQualifiedName=dev.droidchef.usecase.DeleteAllNodes)
Node(id=3, fullyQualifiedName=dev.droidchef.usecase.SetAllNodes)
Node(id=4, fullyQualifiedName=dev.droidchef.usecase.WipeAllNodes)

Using the toHashSet() function

This function will create a new HashSet (which is a data structure that doesn't allow duplicate entries) from the given array.

Points To Remember

  • Return type of toHashSet() is a HashSet<T>
  • Internally it calls toCollection()
  • It does not guarantee the preservation of the iteration order of the elements.

Code Sample

data class Node(val id: Int, val fullyQualifiedName: String)

val nodes = arrayOf(
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(2, "dev.droidchef.usecase.DeleteAllNodes"),
    Node(3, "dev.droidchef.usecase.SetAllNodes"),
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(4, "dev.droidchef.usecase.WipeAllNodes")
)

val deDupedNodes = nodes.toHashSet()
deDupedNodes.forEach {
    println(it)
}

Code Output

Node(id=1, fullyQualifiedName=dev.droidchef.usecase.GetAllNodes)
Node(id=2, fullyQualifiedName=dev.droidchef.usecase.DeleteAllNodes)
Node(id=3, fullyQualifiedName=dev.droidchef.usecase.SetAllNodes)
Node(id=4, fullyQualifiedName=dev.droidchef.usecase.WipeAllNodes)

Using the toMutableSet() function

This function will create a new MutableSet (which is a data structure that doesn't allow duplicate entries) from the given array.

Points To Remember

  • Return type of toMutableSet() is a MutableSet<T>
  • Internally it calls toCollection()
  • It preserves the iteration order of the elements as it uses LinkedHashSet<T> implementation of a MutableSet<T>

Code Sample

data class Node(val id: Int, val fullyQualifiedName: String)

val nodes = arrayOf(
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(2, "dev.droidchef.usecase.DeleteAllNodes"),
    Node(3, "dev.droidchef.usecase.SetAllNodes"),
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(4, "dev.droidchef.usecase.WipeAllNodes")
)

val deDupedNodes = nodes.toMutableSet()
deDupedNodes.forEach {
    println(it)
}

Code Output

Node(id=1, fullyQualifiedName=dev.droidchef.usecase.GetAllNodes)
Node(id=2, fullyQualifiedName=dev.droidchef.usecase.DeleteAllNodes)
Node(id=3, fullyQualifiedName=dev.droidchef.usecase.SetAllNodes)
Node(id=4, fullyQualifiedName=dev.droidchef.usecase.WipeAllNodes)

Using the distinct() function

This function will return a new array that contains only the unique elements from the given array. This is the most flexible way to remove duplicates, as you can still use the original array if you need to. However, it is not the most efficient one.

Points To Remember

  • Return type of distinct() is an Array
  • Internally it calls toMutableSet() followed by toList()
  • Only use this if you want your return type to be an Array.
  • It does not guarantee the preservation of the iteration order of the elements.

Code Sample

data class Node(val id: Int, val fullyQualifiedName: String)

val nodes = arrayOf(
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(2, "dev.droidchef.usecase.DeleteAllNodes"),
    Node(3, "dev.droidchef.usecase.SetAllNodes"),
    Node(1, "dev.droidchef.usecase.GetAllNodes"),
    Node(4, "dev.droidchef.usecase.WipeAllNodes")
)

val deDupedNodes = nodes.distinct()
deDupedNodes.forEach {
    println(it)
}

Code Output

Node(id=1, fullyQualifiedName=dev.droidchef.usecase.GetAllNodes)
Node(id=2, fullyQualifiedName=dev.droidchef.usecase.DeleteAllNodes)
Node(id=3, fullyQualifiedName=dev.droidchef.usecase.SetAllNodes)
Node(id=4, fullyQualifiedName=dev.droidchef.usecase.WipeAllNodes)

Challenges with Removing Duplicates from An Array in Kotlin

One of the common challenges people have when trying to remove duplicates from an array is not knowing which method to use. There are a few different ways to do it, and each has its own benefits and drawbacks. Another challenge is efficiency - if you're working with a large array, you need to make sure that the method you're using is efficient enough to handle it. Otherwise, you could end up with a very slow process.

Conclusion

There are a few different ways to remove duplicates from an array in kotlin, each with its own benefits and drawbacks. The best way to choose which method to use depends on your specific needs. If you're working with a large array, you need to make sure that the method you're using is efficient enough to handle it.

Frequently Asked Questions

Q: What is the best way to remove duplicates from an array in kotlin?

A:  The best way to remove duplicates from an array in kotlin is to use the toSet() method if you don't mind working with a Set<T> and it also preserves the iteration order. If you don't care about the order and only want to have an Array<T> as the return type then you should use distinct()

Q: What are some of the challenges people face when trying to remove duplicates from an array?

A: Some of the challenges people face when trying to remove duplicates from an array include not knowing which method to use and efficiency. If you're working with a large array, you need to make sure that the method you're using is efficient enough to handle it.

Q: What is the most efficient way to remove duplicates from an array in kotlin?

A: The most efficient way to remove duplicates from an array in kotlin is to use the toSetOf() function. This function will create a new Set<T> (which is a data structure that doesn't allow duplicate entries) from the given array. It uses a hashing algorithm to store the elements in the set, so it's very efficient.

I also recently published an article on How to remove duplicates from list in kotlin.

Loading comments...
You've successfully subscribed to Ishan Khanna
Great! Next, complete checkout to get full access to all premium content.
Error! Could not sign up. invalid link.
Welcome back! You've successfully signed in.
Error! Could not sign in. Please try again.
Success! Your account is fully activated, you now have access to all content.
Error! Stripe checkout failed.
Success! Your billing info is updated.
Error! Billing info update failed.