如何删除直方图中的重复字符 [JAVA]

class Mclass {

        public static void main(String[] args) {


        char[] a= {'a','b','c','d','a','b','c'};

        int count = 0;


        for (int i=0; i<a.length; i++)

        {

                  for(int j=0; j<a.length; j++)

                  {

                      if ( a[j] == a[i] )

                          count += 1;

                  }

                  System.out.println(a[i]+":"+count);

                  count = 0;

        }

    }

输出:


a:2

b:2

c:2

d:1

a:2

b:2

c:2


在这里,我想停止循环,直到它计数 d = 1。但它再次打印相同的变量?我怎样才能做到这一点?


偶然的你
浏览 164回答 3
3回答

至尊宝的传说

如果您不想打印已经打印的字符,则需要将其保留在 a 中的某个位置,Set并且仅在Set不包含该字符时打印,并且在打印后将其添加到Set下次病房它不会不会被打印出来。将您的代码更改为此,class Mclass {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; Set<String> doneSet = new HashSet<String>();&nbsp; &nbsp; &nbsp; &nbsp; char[] a = { 'a', 'b', 'c', 'd', 'a', 'b', 'c' };&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < a.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 0; j < a.length; j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (a[j] == a[i])&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!doneSet.contains(String.valueOf(a[i]))) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a[i] + ":" + count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; doneSet.add(String.valueOf(a[i]));&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}这会按照您的意愿提供以下输出,a:2b:2c:2d:1

白板的微信

从你已经做的开始,首先对数组进行排序然后尝试计数import java.util.*;class Mclass {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; &nbsp; &nbsp; char[] a= {'a','b','c','d','a','b','c'};&nbsp; &nbsp; &nbsp; &nbsp; int count = 0;&nbsp; &nbsp; &nbsp; &nbsp; Arrays.sort(a);&nbsp; // sort the array&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0; i<a.length; i++)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=i; j<a.length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( a[j] == a[i] ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; i=j-1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(a[i]+":"+count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}输出a:2b:2c:2d:1

茅侃侃

不要在循环内打印保存您的计数并在循环外打印。做这样的事情:public class Mclass {&nbsp; &nbsp; public static void main(String[] args) {&nbsp; &nbsp; char[] a= {'a','b','c','d','a','b','c'};&nbsp; &nbsp; int count = 0;&nbsp; &nbsp; Map<String,Integer> output = new HashMap<>();&nbsp; &nbsp; for (int i=0; i<a.length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(int j=0; j<a.length; j++)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if ( a[j] == a[i] )&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count += 1;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; output.put(Character.toString(a[i]), count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //System.out.println(a[i]+":"+count);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count = 0;&nbsp; &nbsp; }&nbsp; &nbsp; System.out.println(output);&nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java