Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Find the count of characters that are not repeated in the string in Java [3 ways]

In this post, I will be sharing how to find the Count of characters that are not repeated in the string in Java with examples. There are three ways to achieve our goal:

1. Using indexOf() and lastIndexOf() methods [Easiest]

2. Using Java8

3. Using LinkedHashMap

Read Also: Find the last unique character of a string in Java

Let's dive deep into the topic:

Find the count of characters that are not repeated in the string in Java

1. Using indexOf() and lastIndexOf() methods [Easiest]


You can find the details about the indexOf() and lastIndexOf() methods here. We can easily count the characters that are not repeated in the string in Java using indexOf() and lastIndexOf() methods as shown below in the example:

public class CountNonRepeatedCharacter {
public static void main(String args[]) {
String givenString = "Alive is Awesome";
int count = 0;
for (char ch : givenString.toCharArray())
{
if(givenString.indexOf(ch) == givenString.lastIndexOf(ch))
count++;
}
System.out.println("Non Repeated Character Count is: " + count);
}
}


Output:
Non Repeated Character count is: 5



2. Using Java 8


You can count the characters that are not repeated in the string using Java 8 also as shown below in the example:

import java.util.Map;
import java.util.LinkedHashMap;

public class CountNonRepeatedCharacter2 {
public static void main(String args[]) {
String givenString = "Be in present";
int count = 0;
MapCharacter,Integer> map = new LinkedHashMap();
for (Character ch : givenString.toCharArray()) {
map.put(ch, map.containsKey(ch) ? map.get(ch) + 1 : 1);
}
Long result = map.entrySet().stream().filter(x -> x.getValue() == 1).count();
System.out.println("Non Repeated Character count is: " + result);
}
}


Output:
Non Repeated Character count is: 6



3. Using LinkedHashMap


You can also use LinkedHashMap to find the count of characters that are not repeated in the string in Java as shown below in the example:

import java.util.Map;
import java.util.HashMap;
import java.util.LinkedHashMap;

public class CountNonRepeatedCharacter3 {
public static void main(String args[]) {
String givenString = "Love Yourself";
int count = 0;
HashMapCharacter,Integer> characterMap =
new LinkedHashMapCharacter ,Integer>();
int length = givenString.length();
for (int i=0;i length;i++)
{
char ch = givenString.charAt(i);
if(characterMap.containsKey(ch))
{
// increment count corresponding to ch
characterMap.put( ch , characterMap.get(ch) +1 );
}
else
{
characterMap.put( ch , 1 ) ;
}
}
for(Map.EntryCharacter,Integer> entry: characterMap.entrySet())
{
if(entry.getValue() == 1)
count++;
}
System.out.println("Non Repeated Character count is: " + count);
}
}


Output:
Non Repeated Character count is: 9


That's all for today. Please mention in the comments if you have any questions related to finding the count of characters that are not repeated in the string in Java


This post first appeared on Java Hungry, please read the originial post: here

Share the post

Find the count of characters that are not repeated in the string in Java [3 ways]

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×