-
Notifications
You must be signed in to change notification settings - Fork 0
/
hr_filter_eles.scala
41 lines (36 loc) · 1.35 KB
/
hr_filter_eles.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import scala.io.StdIn.{readLine, readInt}
object Solution {
def filter_elements(arr: Array[Int], tar: Int): Array[Int] = {
val counts = arr.map(i => arr.filter(_ == i).size)
return arr.zip(counts).filter(_._2 >= tar).map(_._1).distinct
}
def main(args: Array[String]) {
val n_cases = readInt()
for (i <- 0 until n_cases) {
var n_tar = readLine().split(' ')
var n_ele = n_tar(0).toInt
var tar_filter = n_tar(1).toInt
var res = filter_elements(readLine.split(' ').map(_.toInt), tar_filter)
if (res.size > 0) println(res.mkString(" "))
else println(-1)
}
}
}
import scala.io.StdIn.{readLine, readInt}
object Solution {
def filter_elements(arr: Array[Int], tar: Int): Array[Int] = {
val counts = arr.groupBy(identity).mapValues(_.size)
return arr.filter(i => counts(i) >= tar).distinct
}
def main(args: Array[String]) {
val n_cases = readInt()
for (i <- 0 until n_cases) {
var n_tar = readLine().split(' ')
var n_ele = n_tar(0).toInt
var tar_filter = n_tar(1).toInt
var res = filter_elements(readLine.split(' ').map(_.toInt), tar_filter)
if (res.size > 0) println(res.mkString(" "))
else println(-1)
}
}
}