java 查找字符串中首个重复出现的字符算法

public String getTheCharOneDis(String str)
    {
        String string = null;
        char[] ch = str.toCharArray();
        // 利用java特性,hashMap中key值不可以重复
        HashMap<Character, Integer> hashMap = new HashMap<Character, Integer>();
        for (int i = 0; i < str.length(); i++)
        {
            if (hashMap.containsKey(ch[i]))
            {
                string = String.valueOf(ch[i]);
                break;
            }
            else
            {
                hashMap.put(ch[i], Integer.valueOf(1));
            }
        }
        return string;
    }


public String findStr(char[] ch)
    {
        // 类似冒泡排序,一个一个查询
        String str = null;
        int length = ch.length;
        for (int i = 0; i < length; i++)
        {
            for (int j = i + 1; j < length; j++)
            {
                if (ch[i] == ch[j])
                {
                    str = String.valueOf(ch[i]);
                    break;
                }
            }
        }
        return str;
    }

你可能感兴趣的:(java)