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

**Task**

Given a list of *N* integers *A = [a _{1}, a_{2}, …, a_{N}]*, 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 = [a _{1}, a_{2}, …, a_{N}]*.

**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 <=Â
*a*Â <= 10_{i}^{9}

**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.