Persistent Coding Question:- Count Non-repeated Character in String

Statement

A data compression software utilizes various steps to compress a string of data. One of the steps involves finding the count of characters that are not repeated in the string.

Write an algorithm for the software developer to find …


This content originally appeared on DEV Community and was authored by Gourav Kadu

Statement

A data compression software utilizes various steps to compress a string of data. One of the steps involves finding the count of characters that are not repeated in the string.

Write an algorithm for the software developer to find the count of characters that are not repeated in the string.

Input Format

The input consists of a string.
compString representing the string
to be compressed.

Output

Print an integer representing the count of characters that are not repeated in the string. If no such character is found or the input string is empty then print 0.

Note

The input string compString is case sensitive. Uppercase characters and lowercase characters are counted as different. The input string compString consists of alphanumeric and special characters only.

Example

Input:

alphaadida

Output:

4

Explanation

** Non repeated characters are l, p ,h ,i **

Solution 1

To edit the code click here

import java.util.*;
// number of repeated character in string
public class nikhil_persistent {

    public static int nonRepeatedChar(String compString) {
        int len = compString.length();
               int arr[]=new int[len];
            int n_arr[]=new int[len];
               int count = 0;
    for(int i = 0 ; i < len ; i++) {
        for(int j=0 ; j < len ; j++) {
        if(compString.charAt(i)==compString.charAt(j)) { //Comparing each char to itself and rest of element in array
        count++; //counting number of occurances
        }
        }
        n_arr[i] = count; // storing all occurances
        count =0;
    }
    for(int i = 0 ; i< len ; i++) {
        if(n_arr[i] == 1) {  // if the element is has only one occurance i.e. unique element 
            count++;
        }
    //System.out.print(n_arr[i]+" " );
    }
        return count; // returning count of uniq element

}
    public static void main(String args[]) 
    {
        Scanner in= new Scanner(System.in);

        String str = in.nextLine();
        System.out.println( nonRepeatedChar(str));

    }

}

Solution 2

To edit the code click here

import java.util.*;
// number of repeated character in string
public class nikhil_persistent {

    public static int nonRepeatedChar(String compString) {
               int arr[]=new int[256];
        for(int i=0;i<compString.length();i++)
        {
            if(compString.charAt(i)!=' ')
               arr[compString.charAt(i)]++;
        }
        int count=0;
       for(int i=0;i<compString.length();i++)
        {
            if(arr[compString.charAt(i)]==1){
            count++;
            }
        }
        return count;
    }
    public static void main(String args[]) 
    {
        Scanner in= new Scanner(System.in);

        String str = in.nextLine();
        System.out.println( nonRepeatedChar(str));

    }

}


This content originally appeared on DEV Community and was authored by Gourav Kadu


Print Share Comment Cite Upload Translate Updates
APA

Gourav Kadu | Sciencx (2021-11-15T10:02:12+00:00) Persistent Coding Question:- Count Non-repeated Character in String. Retrieved from https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/

MLA
" » Persistent Coding Question:- Count Non-repeated Character in String." Gourav Kadu | Sciencx - Monday November 15, 2021, https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/
HARVARD
Gourav Kadu | Sciencx Monday November 15, 2021 » Persistent Coding Question:- Count Non-repeated Character in String., viewed ,<https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/>
VANCOUVER
Gourav Kadu | Sciencx - » Persistent Coding Question:- Count Non-repeated Character in String. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/
CHICAGO
" » Persistent Coding Question:- Count Non-repeated Character in String." Gourav Kadu | Sciencx - Accessed . https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/
IEEE
" » Persistent Coding Question:- Count Non-repeated Character in String." Gourav Kadu | Sciencx [Online]. Available: https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/. [Accessed: ]
rf:citation
» Persistent Coding Question:- Count Non-repeated Character in String | Gourav Kadu | Sciencx | https://www.scien.cx/2021/11/15/persistent-coding-question-count-non-repeated-character-in-string/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.