I have a string, for example 210132. How can I find the second repeating symbol in the string (in this case it is 2)?
Asked
Active
Viewed 182 times
-3
-
1I think there are many algorithm available here for duplicate chat in string. Still find my solution below in answer section in java. you can convert it into python – Aniruddha Das Oct 09 '16 at 15:13
-
1Have you looked anywhere online for an answer? When I type "find duplicate character in string python" into Google I get links to several solutions. – MichaelMaggs Oct 09 '16 at 15:19
-
See also http://stackoverflow.com/questions/32090058/testing-whether-a-string-has-repeated-characters?rq=1 – MichaelMaggs Oct 09 '16 at 15:27
-
@MichaelMaggs , the thing you are suggesting will be the solution if my task is either to find all repeating symbols and/or how much times they all repeat,while my aim is to find just the **second** repeating symbol – CommanderPrice Oct 09 '16 at 16:16
-
I'm voting to close this question as off-topic because it's a zero-effort requirements dump. – EJoshuaS - Stand with Ukraine Jul 20 '17 at 16:39
2 Answers
1
Loop through each character in the string starting from the second character (because the first character can't be a duplicate), keeping track of the current position.
Search for that character in the the portion of the string before that position.
for pos, ch in enumerate(mystring[1:]):
if ch in mystring[:pos+1]:
print ch

John Gordon
- 29,573
- 7
- 33
- 58
-
thanks a lot, that's 95% what I needed.The only problem is that if the symbol appears n>=3 times the output shows this symbol n-1 times.How to print the repeated symbol only 1 time? – CommanderPrice Oct 09 '16 at 16:25
-
@NightStallion. This algorithm will find `1` before it finds `2` in the string `210132`. Is that what you wanted? – ekhumoro Oct 09 '16 at 16:26
-
@ekhumoro,not exactly, but at least it shows the order of repeating – CommanderPrice Oct 09 '16 at 16:28
-
@NightStallion. So you want to find the *first* character in the string that repeats, and then get the position of the repeated character? So for `210132`, it would print `5` (counting from zero). – ekhumoro Oct 09 '16 at 16:31
-
@ekhumoro, no,no, I want to find the _second_ repeating character and to print it.In the string *210132* *1* repeats first and *2* repeats the second and I want to output 2.Is my idea clear? – CommanderPrice Oct 09 '16 at 16:36
-
If you want to print each repeated character only once, you could keep track of already-printed characters by keeping them in a set, and then when you find a repeat, skip it if it's already in the set. – John Gordon Oct 09 '16 at 18:07
0
Finally I solved my task,thanks @John Gordon for idea of using enumerate function
print('Enter string')
while 1 > 0:
string = input()
if string == 'stop':
break
result = list()
for pos, ch in enumerate(string[1:]):
if ch in string[:pos+1]:
result.append(ch)
if len(result) < 2:
print('No second repeating character.Enter another string')
continue
print(result[1],'is the second repeating character')

CommanderPrice
- 49
- 7