Prime

Prepinsta Prime

Video courses for company/skill based Preparation

(Check all courses)
Get Prime Video
Prime

Prepinsta Prime

Purchase mock tests for company/skill building

(Check all mocks)
Get Prime mock

Java program to find non repeating characters in a string

Find non repeating characters in a string

In this article we will learn how to code a java program to find  non-repeating characters in a string. First we will calculate the frequency of each character present in the string as non-repeating characters  are those that are present in the string only once.

Algorithm :

• Take String as a input from the user.
• Create array of 256 element to store frequency of each character.
• Iterate a loop from 0 to length of the string to calculate frequency of each character.
• Again iterate a loop from 0 to 256 to find the character whose frequency is 1 and also print that character.
• we will get the desired output.

Solution in Java for finding the non repeating characters in a string

`import java.util.*;class Solution {    public static void main (String[]args)     {        Scanner sc = new Scanner (System.in);        System.out.println ("Enter the string");        String str = sc.next ();	//Taking input as a string from the user        int freq[] = new int[256];            //Calculating frequency of each character        for (int i = 0; i < str.length (); i++)            freq[str.charAt (i)]++;             System.out.println ("The non repeating characters are : ");            for (int i = 0; i < 256; i++)            if (freq[i] == 1)	//finding the character whose frequency is 1	            System.out.print ((char) i + " ");    } }`

Output :

`Enter the stringprepinstaThe non repeating characters are :a e i n r s t`