Approach : One Liner Java 8 Solution
The similar problem can be solved in Java 8 stream API using one-liner solution as follows –
Java
/*package whatever //do not write package name here */ import java.io.*; import java.util.LinkedHashMap; import java.util.function.Function; import java.util.stream.Collectors; class GFG { private static char findFirstNonRepeatingCharacter(String s) { return s.chars() .mapToObj(x -> Character.toLowerCase(( char ) x)) .collect(Collectors.groupingBy(Function.identity(), LinkedHashMap:: new , Collectors.counting())) .entrySet() .stream() .filter(x -> x.getValue() == 1 ) .findFirst() .orElseThrow() .getKey(); } public static void main (String[] args) { System.out.println(findFirstNonRepeatingCharacter( "w3wiki" )); } } |
Time Complexity : O(n)
Space Complexity : O(n) //As internally it is using a new Map to store data
Related Problem: K’th Non-repeating Character
Find first non-repeating character of given String
Given a string S consisting of lowercase Latin Letters, the task is to find the first non-repeating character in S.
Examples:
Input: “w3wiki”
Output: f
Explanation: As ‘f’ is first character in the string which does not repeat.Input: “algorithm”
Output: a
Explanation: As ‘a’ is first character in the string which does not repeat.
Naive Approach:
The idea is to loop over the string and for every character check the occurrence of the same character in the string. If the count of its occurrence is 1 then return that character. Otherwise, search for the remaining characters.
Note: In python to find the occurrence of a character in the string there is an In-Built Function string.count().
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h> using namespace std; int main() { string string = "w3wikifor" ; int index = -1; char fnc = ' ' ; if (string.size()==0){ cout<< "EMPTY STRING" <<endl; } for ( auto i : string) { if (count(string.begin(), string.end(), i) == 1) { fnc = i; break ; } else { index += 1; } } if (index == string.size()-1) { cout << "All characters are repeating" << endl; } else { cout << "First non-repeating character is " << fnc << endl; } return 0; } // This code is contributed by aakansharao1111 |
Java
//Java code for the above approach import java.io.*; public class GFG { public static void main(String[] args) { String string = "w3wiki" ; int index = - 1 ; char fnc = ' ' ; if (string.length()== 0 ){ System.out.println( "EMPTY STRING" ); } for ( char i : string.toCharArray()) { if (string.indexOf(i) == string.lastIndexOf(i)) { fnc = i; break ; } else { index += 1 ; } } if (index == string.length()- 1 ) { System.out.println( "All characters are repeating" ); } else { System.out.println( "First non-repeating character is " + fnc); } } } // This code is contributed by aakansharao1111 |
Python3
# Python program to print the first non-repeating character string = "w3wiki" index = - 1 fnc = "" if len (string) = = 0 : print ( "EMTPY STRING" ); for i in string: if string.count(i) = = 1 : fnc + = i break else : index + = 1 if index = = len (string) - 1 : print ( "All characters are repeating " ) else : print ( "First non-repeating character is" , fnc) #// This code is contributed by aakansharao1111 |
C#
// C# implementation of the above approach using System; using System.Collections.Generic; using System.Linq; class Gfg { public static void Main( string [] args) { string str = "w3wiki" ; int index = -1; char fnc = ' ' ; if (str.Length == 0){ Console.WriteLine ( "EMPTY STRING" ); } foreach ( char i in str) { if (str.Count(t => t == i)==1){ fnc = i; break ; } else { index += 1; } } if (index == str.Length-1) { Console.WriteLine( "All characters are repeating " ); } else { Console.WriteLine( "First non-repeating character is " + fnc); } } } // This code is contributed by aakansharao1111 |
Javascript
const string = "w3wiki" ; let index = -1; let fnc = ' ' ; if (string.length == 0){ console.log( "EMPTY STRING" ); } for (let i of string) { if (string.split(i).length - 1 === 1) { fnc = i; break ; } else { index += 1; } } if (index === string.length-1) { console.log( "All characters are repeating." ); } else { console.log(`First non-repeating character is ${fnc}`); } // This code is contributed by aakansharao1111 |
All characters are repeating
Time Complexity: O(N2), Traverse over the string for every character in the string of size N.
Auxiliary Space: O(1)