You could use the isMatch function to look in your list of names to see if it already exists before appending if it doesn’t. So if you are wanting to change $names and potentially append $newname to it, try something like this
if (isMatch($names, '.*\b($newname)\b.*')) then
$names
else
$names + ' ' + $newname
isMatch lets us use a regular expression, so here we look for zero or more characters before a word boundary, then the name you want to see is in your list of names, followed by another word boundary.
https://docs.mendix.com/refguide/string-function-calls/#12-ismatch
Hope this helps.