I came across the majority function while falling into a Wikipedia link rabbit hole (I wish I could remember how I got there...)
The function is defined for $n$ arguments and given by $$\mathrm{maj}(x_1,\ldots,x_n)=\left\lfloor\frac{1}{2}+\frac{\sum\limits_{i=1}^n x_n-\frac{1}{2}}{n}\right\rfloor$$ It's easy enough to define the function for a predefined list. For example, given the list
t = RandomInteger[{0, 1}, 10]
(* {0, 0, 1, 1, 1, 0, 0, 1, 1, 1} *)
I can compute the "majority" with
Floor[1/2 + (Sum[t[[i]], {i, 1, Length[t]}] - 1/2)/Length[t]]
(* 1 *)
Is there a way to define $\mathrm{maj}$ in a way similar to, say, f[x_, y_]
?
Majority
(which is a special case ofBooleanCountingFunction
). ConsiderMajority@@(# == 1 & /@ IntegerDigits[num, 2])
. $\endgroup$