Selecting elements by frequency

This function allows the selection of all elements which occur with frequency n in a list. You can choose to get all of the elements or a list containing one of each value which occurs with frequency n. The function takes the option ReducedList which is by default set to True. Setting it to False will give each element n times. The default value for n is one.

The function

ElemCount[x_List,n_:1,opts_?OptionQ]:=Module[{ecopts},
ecopts = ReducedList/.{opts}/.ReducedList-> True;
If[ecopts ===True,Union[Part[x,Flatten[Position[Evaluate[Table[ Count[x,x[[i]]],{i,1,Length[x]}],n]]]]],Part[x,Flatten[Position[Evaluate[Table[ Count[x,x[[i]]],{i,1,Length[x]}],n]]]]]
]

Examples

test={1,2,3,3,4,5,5,6,6,6,7,8,8,8};

This gives a list of all the elements that appear only once.

ElemCount[test]

This gives a list of all elements that appear exactly twice in the list.

ElemCount[test,2]

This gives all occurances of the elements that appear twice in the list.

ElemCount[test,2,ReducedList -> False]