# 4.2.2.5. Counting constraints¶

These constraints count and restrict how many times certain values occur in an array of variables. MiniZinc will automatically generate the basic counting constraints below from expressions such as count(i in x)(i=c) <= d, so you can write models in this much more readable style instead of using these predicates. However, if your model contains multiple counting constraints over the same array, constraints like distribute or global_cardinality below may be useful.

## among¶

 1. predicate among(var int: n, array [$X] of var $$E: x, set of$$E: v) 2. function var int: among(array [$X] of var $$E: x, set of$$E: v)  Requires exactly n variables in x to take one of the values in v. Returns the number of variables in x that take one of the values in v.

## count_geq¶

 predicate count_geq(array [$X] of var $$E: x, var$$E: y, var int: c) predicate count_geq(array [$X] of var $$E: x,$$E: y, int: c)  Constrains c to be greater than or equal to the number of occurrences of y in x.

## count_gt¶

 predicate count_gt(array [$X] of var $$E: x, var$$E: y, var int: c) predicate count_gt(array [$X] of var $$E: x,$$E: y, int: c)  Constrains c to be strictly greater than the number of occurrences of y in x.

## count_leq¶

 predicate count_leq(array [$X] of var $$E: x, var$$E: y, var int: c) predicate count_leq(array [$X] of var $$E: x,$$E: y, int: c)  Constrains c to be less than or equal to the number of occurrences of y in x.

## count_lt¶

 predicate count_lt(array [$X] of var $$E: x, var$$E: y, var int: c) predicate count_lt(array [$X] of var $$E: x,$$E: y, int: c)  Constrains c to be strictly less than the number of occurrences of y in x.

## count_neq¶

 predicate count_neq(array [$X] of var $$E: x, var$$E: y, var int: c) predicate count_neq(array [$X] of var $$E: x,$$E: y, int: c)  Constrains c to be not equal to the number of occurrences of y in x.

## distribute¶

 1. predicate distribute(array [$X] of var int: card, array [$X] of var int: value, array [$Y] of var int: base) 2. function array [$X] of var int: distribute(array [$X] of var int: value, array [$Y] of var int: base)  Requires that card[i] is the number of occurrences of value[i] in base. The values in value need not be distinct. Returns an array of the number of occurrences of value[i] in base. The values in value need not be distinct.

## global_cardinality_closed¶

 1. predicate global_cardinality_closed(array [$X] of var int: x, array [$Y] of int: cover, array [$Y] of var int: counts) 2. predicate global_cardinality_closed(array [$X] of var opt int: x, array [$Y] of int: cover, array [$Y] of var int: counts) 3. predicate global_cardinality_closed(array [$X] of var int: x, array [$Y] of int: cover, array [$Y] of int: lbound, array [$Y] of int: ubound) 4. predicate global_cardinality_closed(array [$X] of var opt int: x, array [$Y] of int: cover, array [$Y] of int: lbound, array [$Y] of int: ubound) 5. function array [$Y] of var int: global_cardinality_closed(array [$X] of var int: x, array [\$Y] of int: cover)  Requires that the number of occurrences of cover[i] in x is counts[i]. The elements of x must take their values from cover. Requires that the number of occurrences of cover[i] in x is counts[i]. The elements of x must take their values from cover or be absent. Requires that for all i, the value cover[i] appears at least lbound[i] and at most ubound[i] times in the array x. The elements of x must take their values from cover. Requires that for all i, the value cover[i] appears at least lbound[i] and at most ubound[i] times in the array x. The elements of x must take their values from cover or be absent. Returns an array with number of occurrences of cover[i] in x. The elements of x must take their values from cover.