Let $\mathfrak{A}$ be an algebra (in the sense of universal algebra). To each term $t(x_1,...,x_n)$ in the language of $\mathfrak{A}$ *in which each variable actually appears* we can assign a group $G_\mathfrak{A}(t)\subseteq S_n$ consisting of all permutations of the variables which results in the same function: $$G_\mathfrak{A}(t)=\{\sigma\in S_n: \forall a_1,...,a_n\in\mathfrak{A}(t(a_1,...,a_n)=t(a_{\sigma(1)},...,a_{\sigma(n)})\}.$$

Now let $\mathbb{G}(\mathfrak{A})$ be the class of *isomorphism types of* groups of the form $G_\mathfrak{A}(t)$ for some term $t$. I'm curious which classes of groups can arise this way.

Originally I asked for a complete answer to this question, but in retrospect that was overly ambitious. To keep things reasonably answerable, let me restrict to the following natural candidates *(playing fast and loose with isomorphism-type-vs.-group issues for simplicity)* - I would love an answer, or even partial progress, to either question (or anything similar really, I'm profoundly stuck-in-the-weeds here):

> **Q1**: Is there an $\mathfrak{A}$ such that $\mathbb{G}(\mathfrak{A})$ consists exactly of the finite [$p$-groups](https://en.wikipedia.org/wiki/P-group) for some prime $p$?

Certainly it's possible to get *only* $2$-groups - see e.g. [this MSE answer of Eric Wofsey](https://math.stackexchange.com/a/4337962/28111). However, getting *exactly* the $2$-groups, or $p$-groups for any fixed $p$, seems much harder. 

> **Q2**: Is there a $\mathfrak{B}$ such that $\mathbb{G}(\mathfrak{B})$ consists exactly of the finite abelian groups?

I really have no relevant information for this question, but it seems like a natural one to ask.

Note that Keith Kearnes' answer [below](https://mathoverflow.net/a/412571/8133) addresses a variant of this question, in which variable *appearance* is replaced by variable *dependence*, and does not seem to immediately generalize to address this version.

****

Here are some example $\mathbb{G}(\mathfrak{A})$s (ignoring up-to-isomorphism details):

 - If $\mathfrak{A}=(A;\star)$ where $\star:A^2\rightarrow A$ is a bijection, then $\mathbb{G}(\mathfrak{A})$ consists only of the trivial group.

 - If $\mathfrak{A}=(\mathbb{N};\max)$, then $\mathbb{G}(\mathfrak{A})=\{S_n:n\in\mathbb{N}\}$.

 - Although in the natural numbers with exponentiation alone [there is essentially only one nontrivial equation](https://math.stackexchange.com/questions/2078937/what-nice-properties-does-exponentiation-have/2080920#2080920), it turns out that *every* finite group shows up - see [here](https://mathoverflow.net/a/414021/8133).

 - One of the few *negative* results I know is that the class of finite cyclic groups is **not** of the form $\mathbb{G}(\mathfrak{A})$ for any $\mathfrak{A}$. To see this, suppose $t(x_1,...,x_k)$ is a $\mathfrak{A}$-term with $G_\mathfrak{A}(t)\cong C_2$. Then the term $$s(x_{1,1},...,x_{k,k}):=t(t(x_{1,1},...,x_{1,k}), ..., t(x_{k,1},...,x_{k,k}))$$ has $G_\mathfrak{A}(s)$ having too many elements of order $2$ to be cyclic. In fact, tweaking this argument we get that $\mathbb{G}(\mathfrak{A})$ consists entirely of cyclic groups iff $\mathbb{G}(\mathfrak{A})$ consists only of trivial groups. However, this sort of idea doesn't seem to be useful for either Q1 or Q2 above.