[Java]在没有hashmap和set的字符串中查找重复的字符

时间:2016-11-24 15:41:50

标签: java string

我编写了一个Java程序来查找字符串中没有Hashmap并设置的重复字符。

以下是该计划,

package practice;

public class Duplicate {

    public static void main(String[] args) {

        String src= "abcad";

        char[] srcChar= src.toLowerCase().toCharArray();

        int len=srcChar.length;
        int j=0;
        boolean flag=false;
        char ch;

        //      System.out.println("Length of the String is "+len1);
        //      System.out.println("Length of the character array is "+len);


        int k=0;

        for(int i=0;i<len;i++)
        {
            //          System.out.println("i-----> "+i + " and character is "+srcChar[i]);
            for(j=0;j<len;j++)
            {
                //              System.out.println("j-----> "+j + " and character is "+srcChar[j]);
                if(srcChar[i]==srcChar[j])
                {
                    k++;
                }
            }
            if(k>1)
            {
                if(srcChar[i]>1)
                {
                    System.out.println("This character "+srcChar[i]+" has repeated "+k+ " time");
                }
                else
                {
                    System.out.println("There are no characters repeated in the given string");
                }
            }
            k=0;
        }
    }
}

这里的输出是:

这个角色a重复了2次 这个角色a重复了2次

在这里,我希望输出像 此字符a已重复2次

即。不重复输出两次。由于字符“a”重复两次,输出也会重复两次。

请帮助我输出一次而不是两次。

谢谢,

7 个答案:

答案 0 :(得分:1)

private static void duplicateChar(String str){
        char[] arr1 = str.toUpperCase().toCharArray();
        int length = str.length();
        int count = 1;
        String s = "";
        char c1 = '\u0000';

        for(int i=0;i<length;i++){
            count = 1;
            for(int j=i+1;j<length;j++){
                if(arr1[i] == arr1[j]){
                    count++;
                    c1 = arr1[i];
                }
                if(j == (length-1) && c1 != '\u0000' && !s.contains(String.valueOf(c1))){
                    s = s+" "+String.valueOf(c1)+"  No of times:  "+count+"\n";
                }
            }
        }
        System.out.println("\nDuplicate char are:\n"+s);
    }

答案 1 :(得分:0)

你可以制作一个2维数组,2个宽,源字符串高度。在此数组中,您可以在替换字符时存储该字符,并将其添加到替换它的次数。

类似的东西(我不知道这些计数器是否正确):

replacements[j][0] = charAt(j);
replacements[j][1] += 1;

您必须检查您要替换的字符是否已存在于此数组中,并且只能打印数组中的元素(如果它们不为空。)

您在原始循环后打印它。

答案 2 :(得分:0)

您需要修复的只是从i而不是0开始第二个循环。

for (int i = 0; i < len; i++) {
    for (j = i; j < len; j++) {
        ...
    }
    ...
}

答案 3 :(得分:0)

<强>进口:

import java.util.ArrayList;
import java.util.List;

<强>代码:

public static void main(String args[]) {
    String input = "abcad"; // Input value
    char[] chars = input.toLowerCase().toCharArray(); // Creates ArrayList
                                                        // of all characters
                                                        // in the String
    List<Character> charR = new ArrayList<>(); // Creates a List used to
                                                // saving the Characters it
                                                // has saved
    List<Integer> valR = new ArrayList<>(); // Creates a List that will
                                            // store how many times a
                                            // character is repeated
    for (int i = 0; i < chars.length; i++) { // Loop through items in the
                                                // ArrayList
        char c = chars[i]; // Create Character value containing the value of
                            // the item at the "i" index of the ArrayList
        if (charR.contains(c)) { // If the List contains item...
            for (int i2 = 0; i2 < charR.size(); i2++) { // Loop through its
                                                        // items
                if (charR.get(i2).equals(c)) { // If you find a match...
                    valR.set(i2, valR.get(i2) + 1); // Increase repeated
                                                    // value by 1
                    i2 = charR.size(); // Stop loop
                } else { // Else...
                    i2++; // Increase index by 1
                }
            }
        } else { // Else...
            charR.add(c); // Add the Character to the List
            valR.add(1); // Add the value 1 to the List (Meaning that the
                            // Character repeated once)
        }
    }
    for (int i = 0; i < charR.size(); i++) { // Loop through all the items
                                                // in the List
        System.out.println("'" + charR.get(i) + "' : " + valR.get(i)); // Display
                                                                        // what
                                                                        // the
                                                                        // character
                                                                        // is
                                                                        // and
                                                                        // how
                                                                        // many
                                                                        // times
                                                                        // it
                                                                        // was
                                                                        // repeated
    }
}

<强>输出:

'a' : 2
'b' : 1
'c' : 1
'd' : 1

答案 4 :(得分:0)

class PrintDuplicateCharacter 
{
    public static void main(String[] args) 
    {   

        String str = "HelloJava";
        char[] ch = str.toCharArray();
        int i=0,j=0;
        for(i=0;i<ch.length;i++)
        {
            int count = 0 ;
            for( j = i+1;j<ch.length;j++)
            {//      4          6 , 8 , 10
                if(ch[i] == ch[j] )                                           
                {
                    count++;
                }
            }
            if(count != 0)           
            {
                System.out.print(str.charAt(i) + " Occured " + count + " time");
            }


        }



    }
}

答案 5 :(得分:0)

   char[] array=value.toCharArray();

   int count=0;

   char ch;
    for(int i=0;i<array.length-1;i++)
    {
        ch=array[i];
        count=1;
        if(ch!='#'){

            for(int j=i+1;j<array.length;j++)
            {
                if(ch==array[j]){
                    count++;
                    array[j]='#';
                }

            }
            if(count>1)
            {
                System.out.println("char is " + ch + "count"  +  count);
            }
        }


    }

答案 6 :(得分:0)

您还可以使用以下代码来解决此问题:

public static void main(String [] args){

    String src = "abcad";

    char[] srcChar = src.toLowerCase().toCharArray();

    int len = srcChar.length;
    int j = 0;
    boolean flag = false;
    char ch;

    // System.out.println("Length of the String is "+len1);
    // System.out.println("Length of the character array is "+len);

    int k = 0;

    for (int i = 0; i < len; i++) {
        // System.out.println("i-----> "+i + " and character is "+srcChar[i]);
        for (j = 0 + i; j < len; j++) {
            // System.out.println("j-----> "+j + " and character is "+srcChar[j]);
            if (srcChar[i] == srcChar[j]) {
                k++;
            }
        }
        if (k > 1) {
            if (srcChar[i] > 1) {
                System.out.println("This character " + srcChar[i] + " has repeated " + k + " time");
            } else {
                System.out.println("There are no characters repeated in the given string");
            }
        }
        k = 0;
    }
}

我们只需要从j = 0 + i开始内部循环;    对于(j = 0 + i; j

这将使您可以观察上面的代码;