Java 程序:查找字符串中重复字符

原文:https://www.studytonight.com/java-programs/java-program-to-find-the-duplicate-characters-in-a-string

在本教程中,我们将学习如何在字符串中查找重复字符。但是在进一步深入之前,如果你不熟悉字符串的概念,那么一定要查看 Java 中Strings的文章。

输入:输入字符串:最大出现次数

输出:字符串中的重复字符有:

m

u

c

r

e

程序 1:查找字符串中的重复字符

在这个程序中,我们将看到当字符串被预定义时,如何在字符串中找到重复的字符。

算法

  1. 开始
  2. 声明字符串
  3. 初始化它
  4. 声明一个变量来计算字符的频率。
  5. 将字符串转换为字符数组。
  6. 使用两个 for 循环来计算字符串中每个元素的频率。
  7. 使用第一个 for 循环来保存字符串的字符。
  8. 将计数初始化为 1。
  9. 使用内部 for 循环迭代字符串中的其余字符。
  10. 检查字符串中是否再次出现该字符。
  11. 如果发生,则增加计数。
  12. 将 str[j]设置为 0,以避免计算访问字符。
  13. 检查计数值是否大于 1,str[i]是否不等于 0。
  14. 如果它满足上述条件,则打印该元素。
  15. 停下来。

下面是相同的 Java 语言代码。

//Java Program to find the duplicate characters in a given string
public class Main
{  
     public static void main(String[] args) 
     {  
        String str1 = "Maximum and Minimum";  
        int count;  
        System.out.println("The entered string is: "+str1);
        //Converts given string into character array  
        char str[] = str1.toCharArray();  

        System.out.println("Duplicate characters in a given string: ");  
        //Count the frequency of each character present in the string  
        for(int i = 0; i <str.length; i++) 
        {  
            count = 1;  
            for(int j = i+1; j <str.length; j++) 
            {  
                if(str[i] == str[j] && str[i] != ' ') 
                {  
                    count++;  
                    //Set string[j] to 0 to avoid printing visited character  
                    str[j] = '0';  
                }  
            }  
            //A character is considered as duplicate if count is greater than 1  
            if(count > 1 && str[i] != '0')  
                System.out.println(str[i]);  
        }  
    }  
}

输入的字符串是:给定字符串中的最大和最小 重复字符: M a i m u n

程序 2:查找字符串中的重复字符

在这个程序中,我们将看到当字符串是用户定义的时,如何在字符串中找到重复的字符。在这里,首先,我们将要求用户输入字符串,然后检查重复的元素。

算法

  1. 开始
  2. 声明字符串
  3. 要求用户初始化它
  4. 声明一个变量来计算字符的频率。
  5. 将字符串转换为字符数组。
  6. 使用两个 for 循环来计算字符串中每个元素的频率。
  7. 使用第一个 for 循环来保存字符串的字符。
  8. 将计数初始化为 1。
  9. 使用内部 for 循环迭代字符串中的其余字符。
  10. 检查字符串中是否再次出现该字符。
  11. 如果发生,则增加计数。
  12. 将 str[j]设置为 0,以避免计算访问字符。
  13. 检查计数值是否大于 1,str[i]是否不等于 0。
  14. 如果它满足上述条件,则打印该元素。
  15. 停下来。

下面是相同的 Java 语言代码。

//Java Program to find the duplicate characters in a given string
import java.util.*;
public class Main
{  
     public static void main(String[] args) 
     {  
         //Take input from the user
        Scanner sc=new Scanner(System.in);  
        System.out.println("Enter the string is: ");
        String str1=sc.nextLine();
        int count;  
        //Converts given string into character array  
        char str[] = str1.toCharArray();            
        System.out.println("Duplicate characters in the given string: ");  
        //Counts each character present in the string  
        for(int i = 0; i <str.length; i++) 
        {  
            count = 1;  
            for(int j = i+1; j <str.length; j++) 
            {  
                if(str[i] == str[j] && str[i] != ' ') 
                {  
                    count++;  
                    //Set string[j] to 0 to avoid printing visited character  
                    str[j] = '0';  
                }  
            }  
            //A character is considered as duplicate if count is greater than 1  
            if(count > 1 && str[i] != '0')  
                System.out.println(str[i]);  
        }  
    }  
}

输入字符串为:重复字符 给定字符串中的重复字符: c a t e r