# count the pairs in a set of Data

I have a list of Data, e.g.,

list1={0, 1, 1, 1, 1, 2, 2, 3, 3}

list2={0, 1, 1, 1, 1, 2, 2, 3, 3, 0}


and I want to get as an output True or False if I have only even number of Data of similar or not. in the above example I like to write a code that returns False for list1 and True for list2, since in list1 I have only one 0, but in the list2 there are even number of each number.

Thanks!

## 4 Answers

Using Counts

f = And @@ EvenQ[Values[Counts[#]]] &
{f[list1], f[list2]}


{False, True}

• +1. Or f = Counts /* AllTrue[EvenQ] – WReach Nov 1 '17 at 14:27
• Yours is much concise. I think you should post it as a separate answer. – Anjan Kumar Nov 1 '17 at 14:30
• I did, but when I saw yours, I deleted it :) I shall undelete. – WReach Nov 1 '17 at 14:36

Given:

f = Counts /* AllTrue[EvenQ];


Then:

f[list1]
(* False *)

f[list2]
(* True *)


You can use the Tally function:

Tally[list]
tallies the elements in list, listing all distinct elements together with their multiplicities.

So as

And@@EvenQ[Tally[list1][[;; , 2]]]


or

And@@EvenQ[Tally[list2][[;; , 2]]]

check[list_] := If[
Cases[EvenQ[Count[list, #] & /@ Union[list]], False] == {},
True, False]

check[list2]


True