1

How do I find duplicate values in two different arrays ?

Pseudo-code:

array1["a", "b", "c", "d"]

array2["b", "d", "e", "f"]

duplicatesFound = findDuplicates(array1, array2) //will return ["b", "d"]
Baretka
  • 21
  • 6
  • Does this answer your question? [Intersection of two lists maintaining duplicate values in Kotlin](https://stackoverflow.com/questions/53687530/intersection-of-two-lists-maintaining-duplicate-values-in-kotlin) – Alain Stulz Apr 15 '20 at 13:49

1 Answers1

2

obviously there is more than one way to solve it, here is one: You can use Intersect between two Iterable arrays. for example

    val a1 = arrayListOf("a", "b", "c", "d")
    val a2 = arrayListOf("b", "d", "e", "f")

    val intersect = a2.intersect(a1)

    Log.d(TAG,intersect.toString()) // prints [b,d]
    Log.d(TAG,"${intersect.size}") // prints 2
Ben Shmuel
  • 1,819
  • 1
  • 11
  • 20