我有以下数组:
Array(book, chalk, book, eraser),
Array(book, chalk, book, pen),
Array(book, chalk, book, pencil),
Array(book, chalk, chalk, eraser),
Array(book, chalk, chalk, pen),
Array(book, chalk, chalk, pencil),
Array(book, chalk, eraser, pen)
Array(book, chalk, eraser, pencil),
Array(book, chalk, pen, pencil)
Array(book, eraser, book, pen),
Array(book, eraser, book, pencil),
Array(book, eraser, chalk, eraser)
我想删除具有不同字符串的数组。例如:Array(book,chalk,eraser,pencil)
需要删除。
我该怎么办?
答案 0 :(得分:1)
你可以简单地使用:
val arr = Array(Array("book","chalk", "pencil","eraser"),Array("book","chalk", "book","pen"),Array("book","chalk", "book","pencil"),Array("book","chalk", "chalk","eraser"),Array("book","chalk", "chalk","pen"))
arr.filter(x => x.length != x.toSet.size)
答案 1 :(得分:0)
而不是不同,你可以使用简单的自定义函数,它会更有效率,因为它不应该遍历整个数组,如果复制是在数组的开头:
import scala.collection.mutable
def hasDuplicate[T](arr: Array[T]): Boolean = {
val seen = mutable.HashSet[T]()
var seenBefore = false
var i = 0
while (!seenBefore && i < arr.length ) {
val v = arr(i)
seenBefore = seen.contains(v)
seen += v
i += 1
}
seenBefore
}
val input = Array(Array("book","chalk", "pencil","eraser"),Array("book","chalk", "book","pen"),Array("book","chalk", "book","pencil"),Array("book","chalk", "chalk","eraser"),Array("book","chalk", "chalk","pen"))
input.filterNot(hasDuplicate)