Programming Examples

Java program to remove duplicate character from string


Java program to remove duplicate character from string

Sample:

Input: infomaxcomputeracademy

Output:  infomaxcputerdy

Solution

import java.util.*;   
class RemoveDuplicate  
{ 
    public static void main(String[] args) 
    { 
        Scanner sc=new Scanner(System.in);
        System.out.println("Enter Any String");
        String mystr=sc.nextLine();
        char str[] = mystr.toCharArray(); 
        int n = str.length; 
        int index = 0;   
        for (int i = 0; i < n; i++) 
        { 
            int j; 
            for (j = 0; j < i; j++)  
            { 
                if (str[i] == str[j]) 
                { 
                    break; 
                } 
            } 
            if (j == i)  
            { 
                str[index++] = str[i]; 
            } 
        } 
        System.out.println(String.valueOf(Arrays.copyOf(str, index))); 
    } 
} 
  
Output


Online Exam Quiz for One day Exam Online Typing Test CCC Online Test 2026 Best Computer Training Institute in Prayagraj (Allahabad) Best Java Training Institute in Prayagraj (Allahabad) Best Python Training Institute in Prayagraj (Allahabad) O Level Online Test in Hindi Best Website and Software Company in Allahabad