TITLE: Number of functions $f : A \to A$ such that $f(f(x))=f(x)$
QUESTION [5 upvotes]: If $A=\left\{1,2,3,4,5\right\}$ Then

Find Number of functions $f : A \to A$ such that $f(f(x))=f(x)$

Case $1.$ if $f$ is injective then $f(f(x))=f(x)$ $\implies$ $f(x)=x$, hence there is only one injective function which is an identity function.
Case $2.$ When $f$ is many to one function
Let us assume that $f(1)=f(2)=f(3)=f(4)=f(5)=k$ Then clearly for any $k$ from the set $A$, $f(f(x))=f(x)$
hence there are five such functions.
Are there any other possibilities?

REPLY [3 votes]: Let $B_f = \{x\in A:f(x)=x\}$ and $C_f= \{x\in A: f(x)\ne x\}.$
For every $x\in C,$ the equality $f(f(x)) = f(x)$ implies that $f(x)\in B.$
Thus we can choose a function satisfying $f(f(x))=f(x)$ by

choosing a set $\varnothing\ne B \subseteq A,$ and then
choosing a function $f$ from $C=A\smallsetminus B$ into $B.$

The number of ways to choose a nonempty subset of $A$ is $2^{|A|}-1 = 2^5-1 = 31.$ The number of ways to choose a subset of any particular size $k\in\{1,\ldots,n\} = \{1,\ldots,5\}$ is $\dbinom n k = \dbinom 5 k.$
The number of ways to choose a function from $C$ into $B$ is $|B|^{|C|} = |B|^{|A|-|B|} = |B|^{5-|B|}.$
So the number you seek is
$$
\sum_{k=1}^n \binom n k k^{5-k} = \sum_{k=1}^5 \binom 5 k k^{5-k} = 5 + 80 + 90 + 20 + 1.
$$