Categories
Interview java

Print non repeated characters in java

Print non repeated characters in java

In this example we will print non repeated characters from a string using java 
This is mostly asked in the java programming interview 

Example

import java.util.ArrayList;

public class NonRepeatedCharcters {

  public static void main(String[] args) {
    String s = "BeginnersBug";
    String nonRepeatedChars = "";
    ArrayList arrayList = new ArrayList();
    char[] charArray = s.toCharArray();

    for (int i = 0; i < charArray.length; i++) {
      // Checking that character already exists on array list
      boolean contains = arrayList.contains(charArray[i]);
      if (!contains) {        
        arrayList.add(charArray[i]);
        nonRepeatedChars += charArray[i];
      }
    }
    System.out.println(nonRepeatedChars);
  }
}

Output

Beginrsu

Array List Reference

 https://beginnersbug.com/arraylist-in-java/

Download Source Code

https://github.com/rkumar9090/BeginnersBug/blob/master/BegineersBug/src/com/geeks/example/NonRepeatedCharcters.java