ho can we remove the values corresponding to clouds from the histogram
First you need to identify them by some means. For example with Idepix, based on already provided flags, or some simple expression (probably not good results).
Then you can change the valid pixel-expression for the band you want to create histogram for. Afterwards clouds are not considered anymore