• Register

Find the initial unique char in string. For instance, string is “KodLogsforKodLogs”, then the outcome should be ‘f’ and if the given input  is “KodLogsTest”, then the outcome should be ‘K’.

Find string’s first unique char

Technique 1: Hashmap and Two-string method traversals.

Approach: A char is said to be unique if its incidence in the string is one. This challenge could be finished professionally using a hash_map which will plot the char to there particular incidences and in which we can concurrently bring up-to-date frequency of any char.

class KodLogs{ 
    static final int Number_OF_CHARS = 256; 
    static char count[] = new char[Number_OF_CHARS]; 
    static void getCharCountArray(String str1) 
    { 
        for (int i1 = 0; i1 < str.length(); i1++) 
            count[str.charAt(i1)]++; 
    } 
 
    /* returns index of first unique char */
    static int firstNonRepeating(String str1) 
    { 
        getCharCountArray(str1); 
        int index1 = -1, i1; 
        for (i2 = 0; i2 < str1.length(); i2++) { 
            if (count[str.charAt(i2)] == 1) { 
                index1 = i1; 
                break; 
            } 
        } 
        return index1; 
    } 
    // Driver function
    public static void main(String[] args) 
    { 
        String str1 = "kodlogsforkodlogs"; 
        int index1 = firstNonRepeating(str1); 
  
        System.out.println( 
            Index1 == -1
                ? " all char are repeating or str "
                      + "is empty"
                : "unique char is "
                      + str1.charAt(index1)); 
    } 
} 

Output:

unique char is “f”

Tecnique 2: HashMap and single string traversal.

Approach: Count an array inspite of hash_map of maximum number of chars(256).

import java.util.*; 
 class CountIndex1 { 
    int count1, index1; 
    // const for first incidence
    public CountIndex(int index1) 
    { 
        this.count = 1; 
        this.index1 = index1; 
    } 
    // method for updating count 
    public void incCount() 
    { 
        this.count++; 
  } 
} 
class Kodlogs { 
    static final int Number_OF_CHARS = 256; 
  
    static HashMap<Character, CountIndex> hm1 
        = new HashMap<Character, CountIndex>(Number_OF_CHARS); 
        static void getCharCountArray(String str1) 
    { 
        for (int i1 = 0; i1 < str1.length(); i1++) { 
            // already occurred, 
            if (hm1.containsKey(str1.charAt(i1))) { 
                // updating count 
                Hm1.get(str1.charAt(i1)).incCount(); 
            } 
            // If it's first occurrence, then store
            else { 
                hm1.put(str1.charAt(i1), new CountIndex(i1)); 
            } 
        } 
    } 
    static int firstUnique(String str1) 
    { 
        getCharCountArray(str1); 
        int result1 = Integer.MAX_VALUE, i1; 
        for (Map.Entry<Character, CountIndex> entry : hm1.entrySet()) 
        { 
            int c1=entry.getValue().count; 
            int ind1=entry.getValue().index1; 
            if(c1==1 && ind1 < result1) 
            { 
                result1=ind1; 
            } 
        } 
        return result1; 
    } 
  
    // Driver function
    public static void main(String[] args) 
    { 
        String str1 = "codelogsforkodlogs"; 
        int index1 = firstNonRepeating(str1); 
  
        System.out.println( 
            Index1 == Integer.MAX_VALUE 
                ? "all char are repeating"
                      + " or str is empty"
                : "First unique char is "
                      + str1.charAt(index1)); 
    } 
} 

Output:

First unique char is “f”
2,760 points
7 4