fork download
  1. #include <iostream>
  2. #include <cstring>
  3. #include <limits>
  4.  
  5. int main()
  6. {
  7. constexpr char cstr[] = "introduction";
  8.  
  9. // initialize an array to hold the count of occurrances
  10. int counts[ std::numeric_limits<unsigned char>::max() ] = {0} ;
  11.  
  12. // populate the counts
  13. for( unsigned char u : cstr ) ++counts[u] ;
  14.  
  15. // find the first char for which count is equal to 1
  16. for( unsigned char u : cstr ) if( counts[u] == 1 && u != 0 )
  17. {
  18. std::cout << "first non-repeating char is '" << char(u) << "'\n" ;
  19. break ;
  20. }
  21. }
  22.  
Success #stdin #stdout 0s 2896KB
stdin
Standard input is empty
stdout
first non-repeating char is 'r'