How can I check if an array contains a value of another array?

荒凉一梦 提交于 2021-02-11 15:40:37

问题


I would like to check to see if an array contains a value from another array. For example, I would like to check if the array A contains a value from an array B.

I'm looking for any value not one specific value.


回答1:


If you're wanting to see if there is any overlap at all between two arrays, you can do this:

fun Array<*>.intersects(other: Array<*>) = any { it in other }

As mentioned in the comments below, this is O(n^2) so with large arrays, prefer:

fun Array<*>.intersects(other: Array<*>) = intersect(other.toSet()).isNotEmpty()

toSet() is only needed if the second collection is an Array rather than an Iterable like List.




回答2:


Example:

val a = arrayOf(1, 2, 5, 7, 9, 15)
val b = arrayOf(15, 3, 5, 8)
val c = a intersect b.toList()
if (!c.isEmpty()) {
    println("Arrays have common elements ${c}")
} else {
    println("Arrays do not have common elements")
}

Result:

Arrays have common elements [5, 15]

Implementation intersect in Kotlin use Set:

val set = this.toMutableSet()
set.retainAll(other)
return set

and should be enough for most typical casses, for example if both array have 10 000 000 elements, this take about 8 s.

In case when arrays are very large (e.q. when set of such large number of element doesn't fit in memory), possible solution is do sort arrays, and do something like merge of sorted arrays:

fun hasIntersection(a: IntArray, b: IntArray): Boolean {
    a.sort()
    b.sort()
    var i = 0
    var j = 0
    while (i < a.size && j < b.size) {
        if (a[i] < b[j]) {
            i++
        } else if (a[i] > b[j]) {
            j++
        } else {
            return true
        }
    }
    return false
}



回答3:


Make sure your array A and B are the same type. then try like the following.

for (i in 0..arrayB.size-1) { 
    arrayA.contains(arrayB[i]); 
} 



回答4:


You can use any and contains methods to check if there is a common element.

val a = arrayOf(1, 2, 5, 7, 9, 15)
val b = arrayOf(15, 3, 5, 8)

println(a.any(b::contains))
// true

Please note: This method is not very efficient. It has a worst case complexity of n^2. One way to improve performance is to convert the array on which look up is done (b in this eg) to a Set




回答5:


Return true if any value matches

  private fun ArrayList<Int>.isAnyValueMatched(list2:ArrayList<Int> ):Boolean{
        this.forEach { value1->
            list2.forEach {
                value2->
                if (value1==value2) return true
            }
        }
        return false
    }

Or use intersect( Returns a set containing all elements that are contained by both this array and the specified collection)

private fun ArrayList<Int>.isAnyValueMatched(list2:ArrayList<Int> ):Boolean{
        return (this intersect list2).isNotEmpty()
 }


来源:https://stackoverflow.com/questions/62087791/how-can-i-check-if-an-array-contains-a-value-of-another-array

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!