#include <bits/stdc++.h>
using namespace std;
int leftmost(string s) {
string s1 = " ";
int arr[256] = {0};
for (int i = 0; i < s.length(); i++) {
if (arr[int(s[i])] == 0) {
arr[int(s[i])]++;
s1 = s1 + s[i];
}
if (arr[int(s[i])] == 1) {
arr[int(s[i])]++;
int temp = s1.find(s[i]);
s1.erase(temp, temp);
}
}
return s.find(s1[0]);
}
int main() {
string s = "geeksforgeeks";
int res = leftmost(s);
cout << res;
return 0;
}
This is the code for leftmost non-repeating char in a string which is not giving correct output. It is giving output '1' while the expected output is 'f'. the output remains the same if we change the input string.
– Akmal-threepointsix Aug 15 '22 at 13:08