Swarm density plays a key role in the performance of a robot swarm, which can be averagely measured by swarm size and the area of a workspace. In some scenarios, the swarm workspace may not be fully or partially observable, or the swarm size may decrease over time due to out-of-battery or faulty individuals during operation. This can result in the average swarm density over the whole workspace being unable to be measured or changed in real-time. The swarm performance may not be optimal due to unknown swarm density. If the swarm density is too low, inter-robot communication will rarely be established, and robot swarm cooperation will not be effective. Meanwhile, a densely-packed swarm compels robots to permanently solve collision avoidance issues rather than performing the main task. To address this issue, in this work, the distributed algorithm for collective cognition on the average global density is proposed. The main idea of the proposed algorithm is to help the swarm make a collective decision on whether the current global density is larger, smaller or approximately equal to the desired density. During the estimation process, the swarm size adjustment is acceptable for the proposed method in order to reach the desired swarm density.