Get items from a list that repeat a certain number of times

In a repeating group (named RG-1) I have a list of elements [a, b, c, a, b, a, b, c]. I want to set the data source of another repeating group (named RG-2) to only those values from RG-1 that occur X number of times. How can I do this?

Eg: If RG-1 has the data source as mentioned above and X is 3, then RG-2 should have data as [a,b].

Been bugging me for hours now, any help would be appreciated.

@Adish

More performant way would be to keep a count field in the element object and condition row visibility to it.