Find the first non-repeating character in a string | faceprep

Program to find the first non-repeating character in a string is discussed here. Given a string, the task is to find the first non-repeating character in the string.

Input and Output format:

The first line of the input consists of a string. 

Sample Input1: 

teeterson 

Sample Output1: 

Sample Input2: 
charactercharacter 

Sample Output2: 
All characters are repetitive

Algorithm to find the first non-repeating character in a string

  • Input the string from the user.
  • Start traversing the string using two loops.
  • Use the first loop to scan the characters of the string one by one.
  • Use the second loop to find if the current character is occurring in the latter part if the string or not.
  • If it is not occurring, print that character.
  • Else, continue traversing.

Program to find the first non-repeating character in a string is given below.

/* C program to find first non-repeating character */
#include<stdlib.h>
#include<stdio.h>
#define NO_OF_CHARS 256

int *get_char_count(char *str)
{
int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}

int first_non_repeating_character(char *str)
{
int *count = get_char_count(str);
int index = -1, i;

for (i = 0; *(str+i); i++)
{
if (count[*(str+i)] == 1)
{
index = i;
break;
}
}

free(count);
return index;
}

int main()
{
char str[NO_OF_CHARS];
printf(“\nEnter the string : “);
scanf(“%s”,&str);
int index = first_non_repeating_character(str);
if (index == -1)
printf(“All the characters are repetitive”);
else
printf(“First non-repeating character is %c”, str[index]);
getchar();
return 0;
}

/* C++ program to find first non-repeating character */
#include<stdlib.h>
#include<iostream>
using namespace std;
#define NO_OF_CHARS 256

int *get_char_count(char *str)
{
int *count = (int *)calloc(sizeof(int), NO_OF_CHARS);
int i;
for (i = 0; *(str+i); i++)
count[*(str+i)]++;
return count;
}

int first_non_repeating_character(char *str)
{
int *count = get_char_count(str);
int index = -1, i;

for (i = 0; *(str+i); i++)
{
if (count[*(str+i)] == 1)
{
index = i;
break;
}
}

free(count);
return index;
}

int main()
{
char str[NO_OF_CHARS];
cout << “\nEnter the string : “;
cin >> str;
int index = first_non_repeating_character(str);
if (index == -1)
cout << “All the characters are repetitive”;
else
cout << “First non-repeating character is ” << str[index];
return 0;
}

/* Java program to find first non-repeating character */

import java.util.*;

class Main
{
static final int NO_OF_CHARS = 256;
static char count[] = new char[NO_OF_CHARS];

static void get_char_count(String str)
{
for (int i = 0; i < str.length(); i++)
count[str.charAt(i)]++;
}

static int first_non_repeating_character(String str)
{
get_char_count(str);
int index = -1, i;

for (i = 0; i < str.length(); i++)
{
if (count[str.charAt(i)] == 1)
{
index = i;
break;
}
}

return index;
}

public static void main (String[] args)
{
String str;
Scanner sc = new Scanner(System.in);
str = sc.nextLine();
int index = first_non_repeating_character(str);

System.out.println(index == -1 ? “All the characters are repetitive ” +
“is empty” : “First non-repeating character is ” + str.charAt(index));
}
}

# Python program to find the first non-repeating character in a string

NO_OF_CHARS = 256
def get_char_count(string):
count = [0] * NO_OF_CHARS
for i in string:
count[ord(i)]+=1
return count

def first_non_repeating_character(string):
count = get_char_count(string)
index = -1
k = 0

for i in string:
if count[ord(i)] == 1:
index = k
break
k += 1

return index

string = input(“Enter the string : “)
index = first_non_repeating_character(string)
if index==1:
print (“All the characters are repetitive”)
else:
print (“First non-repeating character is “, string[index] )

Output:

first non-repeating character in a string