# Filter Elements – HackerRank Solution

In this post, we will solve Filter Elements HackerRank Solution. This problem (Filter Elements) is a part of HackerRank Functional Programming series.

Given a list of N integers A = [a1, a2, …, aN], you have to find those integers which are repeated at least K times. In case no such element exists you have to print `-1`.

If there are multiple elements in A which are repeated at least K times, then print these elements ordered by their first occurrence in the list.

Let’s sayÂ A = [4, 5, 2, 5, 4, 3, 1, 3, 4]Â andÂ K = 2. Then the output is

``4 5 3``

because these numbers have appeared at least 2 times.
Among these numbers,
4Â has appeared first at positionÂ 1,
5Â has appeared next at positionÂ 2,
andÂ 3Â has appeared thereafter at positionÂ 6.
That’s why, we print in the orderÂ 4,Â 5Â and finallyÂ 3.

## Input

First line contains an integer,Â T, the number of test cases. ThenÂ TÂ test cases follow.
Each test case consist of two lines. First line will contain two space separated integers,Â NÂ andÂ K, whereÂ NÂ is the size of listÂ A, andÂ KÂ represents the repetition count. In the second line, there areÂ NÂ space separated integers which represent the elements of listÂ A = [a1, a2, …, aN].

## Output

For each test case, you have to print all those integers which have appeared in the list at leastÂ KÂ times in the order of their first appearance, separated by space. If no such element exists, then printÂ `-1`.

## Constraints

• 1 <=Â TÂ <= 10
• 1 <=Â NÂ <= 10000
• 1 <=Â KÂ <= N
• 1 <=Â aiÂ <= 109

Sample Input

``````3
9 2
4 5 2 5 4 3 1 3 4
9 4
4 5 2 5 4 3 1 3 4
10 2
5 4 3 2 1 1 2 3 4 5``````

Sample Output

``````4 5 3
-1
5 4 3 2 1``````

Explanation

Sample Case #01:Â This is the same example mentioned in the problem statement above.
Sample Case #02:Â As no elements repeats more than 3 times, we don’t have any elements satisfying the criteria of minimumÂ KÂ times.
Sample Case #03:Â All elements are repeated 2 times. So we print all of them according to their order of occurance, which is 5 -> 4 -> 3 -> 2 -> 1.

## Solution – Filter Elements – HackerRank Solution

Scala

```import java.util.Scanner

object Solution {
def main(args: Array[String]): Unit = {
val sc = new Scanner(System.in)

val t = sc.nextInt

(0 until t).foreach(_ => {
val n = sc.nextInt
val k = sc.nextInt

val seq = (0 until n).map(_ => sc.nextInt).toList

val numbers = seq.groupBy(identity)
.collect { case (key, list) if list.size >= k => key }
.toSet

println((if (numbers.isEmpty) Seq(-1)
else {
extract(seq, numbers)
}).mkString(" "))
})

sc.close()
}

def extract(seq: List[Int], numbers: Set[Int]): List[Int] = {
@scala.annotation.tailrec
def inner(seq: List[Int], numbers: Set[Int], acc: List[Int]): List[Int] = seq match {
case Nil => acc.reverse
case x :: xs =>
if (numbers.contains(x)) inner(xs, numbers - x, x :: acc)
else inner(xs, numbers, acc)
}

inner(seq, numbers, Nil)
}
}
```

Note: This problem (Filter Elements) is generated by HackerRank but the solution is provided by CodingBroz. This tutorial is only for Educational and Learning purpose.