![Python-courses-python-exercises-python-students-online-courses](https://1.bp.blogspot.com/-T3BongcoJFo/X4CftQxVL8I/AAAAAAAAB5U/Lf59WgNyhGo2FdogKqxv0PIy_Ef7XDPdgCLcBGAsYHQ/s0/my-python-courses-python-exercises-with-solutions.png)
Exercise 75
Write a python algorithm which determines the list of all common characters to two strings s1 and s2 without repetition. Example if s1 = "Python language" and s2 = "Programming", the algorithm returns the liste ['P', 'o', 'n', 'a', 'g'] ( the charcter 'g' must be added only once even if shared twice)
Solution
def commonCharacters(s1 , s2):
# initializing the list of common characters
listCommon = []
for x in s1:
if x in s2 and x not in listCommon:
listCommon.append(x)
return listCommon
# Example
s1 = "Python language"
s2 = "Programming"
print("List of common characters : ", commonCharacters(s1 , s2))
# The output is : List of common characters : ['P', 'o', 'n', 'a', 'g']
Younes Derfoufi
my-courses.net
my-courses.net