如何使用java删除char数组中的重复字符?

时间:2016-11-02 06:42:30

标签: java arrays netbeans char

public static void main(String[] args) 
{

    char[] x = {'b', 'l', 'a', 'h', 'h', ' '};
    char[] y = {'g', 'o', 'g', 'o'};

    System.out.println(removeDuplicate(x, y));
    System.out.println(noDuplicate(y, x));

public static char[] removeDuplicate(char[] first, char[] second)
{  

   //used my append method (didn't enclose) to append the two words together
   char[] total1 = append(first, second);

   //stores character that have been encountered
   char[] norepeat = new char[total1.length];

   int index = 0;

   //store result
   char[] solution = new char[total1.length];

   boolean found = false;

   //for loop keeps running until blahh gogo is over
   for(int i = 0; i < total1.length; i++)
   {
       for(int m = 0; m <norepeat.length; m++)
       {
            if(total1[i] == norepeat[m])
            {  
                found = true;
                break;
            }
   }

    if (!found)
       {   
           norepeat[index] = total1[i];
           index++;

           solution[index] = total1[i];
           index++;
       }
   }

   return solution;
}
}

当前输出:

blah

go

我希望输出为:

blah go

goblah (space at the end)

我的代码的问题是它在遇到第一次重复后停止运行,所以它甚至根本不运行整个单词。我相信它与我的嵌套for循环有关,但我不确定。我试着把它写在纸上,但它似乎没有任何帮助。

任何帮助将不胜感激!谢谢!

4 个答案:

答案 0 :(得分:0)

我认为您需要在第一个循环开始时将找到的变量指定为false。第一次转弯后,找到的值始终为真。

for(int i=0;i<total1.length;i++)
{
 found=false;
 for(int m = 0; m <norepeat.length; m++)
   {      
        if(total1[i] == norepeat[m])
        {  
            found = true;
            break;
   }

您也可以使用字符集而不是使用char数组。集合是一个不允许重复元素的集合。有关详细信息,请参阅。关于套装你可以访问此链接。Sets

答案 1 :(得分:0)

如果可能的字母是ASCII范围,那么你可以使用一个简单的布尔数组来跟踪你已经看过的字母:

boolean[] seen = new boolean[256];

如果不修改原始字符数组,则可以将唯一元素排列到数组的开头,然后创建第一个size元素的新数组并将其返回。

int size = 0;
for (int j = 0; j < chars.length; j++) {
    char c = chars[j];
    if (!seen[c]) {
        chars[size++] = chars[j];
        seen[c] = true;
    }
}
return Arrays.copyOf(chars, size);

如果字母表可以超过ASCII范围,您可以使用Set<Character>来跟踪看到的字符。

答案 2 :(得分:0)

这是我使用Map和List作为助手的解决方案。作为char数组的输入和输出被保留。

请注意,您不应以solution的长度初始化total1,否则char数组将在末尾打印空白。

public static char[] removeDuplicate(char[] first, char[] second){  

   //used my append method (didn't enclose) to append the two words together
   char[] total1 = append(first, second);

   //stores character that have been encountered
   Map<Character,Boolean> norepeat = new HashMap<Character,Boolean>();
   //store partial result
   List<Character> partSolution=new ArrayList<Character>();

    //for loop keeps running until blahh gogo is over
    for(int i = 0; i < total1.length; i++){
        if(! norepeat.containsKey(total1[i])) {   
            norepeat.put(total1[i], true) ;
            partSolution.add(total1[i]);
        }

    }

    //store final result
    char[] solution = new char[partSolution.size()];
    for(int i=0;i<partSolution.size();i++){
       solution[i]=partSolution.get(i);
    }    

    return solution;

}

答案 3 :(得分:-1)

如果您需要摆脱重复的元素并保留数组的顺序,您可以使用LinkedHashSet。这是一个例子:

Set<Character> x = new LinkedHashSet<Character>(Arrays.asList(new Character[]{'b', 'l', 'a', 'h', 'h', ' '}));
Set<Character> y = new LinkedHashSet<Character>(Arrays.asList(new Character[]{'g', 'o', 'g', 'o'}));

System.out.println(x);
System.out.println(y);

<强>输出:

[b, l, a, h,  ]
[g, o]

编辑。以下是如何将char []转换为Character []的示例,反之亦然:

char[] a = new char[]{'b', 'l', 'a', 'h', 'h', ' '};
Character[] boxed = IntStream.range(0, a.length).mapToObj(i -> a[i]).toArray(size -> new Character[size]);
char[] unboxed = IntStream.range(0, boxed.length).mapToObj(i -> String.valueOf(boxed[i])).collect(Collectors.joining()).toCharArray();