ProAnswers.org

How to find first non-repeating character in a string

Given a string how can I find the first character which does not repeats itself?

e.g. In the string “PQRQP”, R is the first non-repeating character.

This is an implementation in javascript




	
		 
	
		
			
				
					
						01
					
						02
					
						03
					
						04
					
						05
					
						06
					
						07
					
						08
					
						09
					
						10
					
						11
					
						12
					
						13
					
						14
					
						15
					
						16
					
						17
					
						18
				
				
					
						
							function firstRepet(str)
						
							    {
						
							        var subStr = str.substr(1);
						
							        if(subStr.indexOf(str[0]) < 0 )
						
							        {
						
							            alert("first non repet is" + str[0])
						
							        }
						
							        else
						
							        {
						
							            for(var i = 0; i
						
							            {
						
							                subStr = str.substr(0,i-1) + str.substr(i+1);
						
							                if(subStr.indexOf(str[i])<0 ) break;
						
							            }
						
							            if(i == str.length) alert("All are repeted");
						
							            else alert("first non repet is " + str[i]);
						
							        }
						
							    }