I want to find all directories containing, for example, files named *.txt. but the output must not contain duplicates. How to do it?
POSIX compatible code which should work for any filename:
find . -name '*.txt' -printf '%h\0' | tr '\0\n' '\n\0' | sort -u | tr '\0\n' '\n\0'
A problem can be that directory names can contain newlines, therefore the output from find should be NUL terminated. In order to have readable output pipe the result of sort through
find . -name "*.txt" -printf '%h\0' | sort -zu | tr '\0' '\n'
Any newline in a directory name can probably be determined by looking at the next line, if it starts with
./ it was not a newline in a directory.