1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

備忘録No.1「配列内の重複した値を数えて表示」[Java]

Last updated at Posted at 2020-10-21

#コード


    public static void main(String[] args) {
        // TODO 自動生成されたメソッド・スタブ

        String [] array = {"y","x","x","y","z","y","x","y","z","z"};
        List<String> list = new ArrayList<>(Arrays.asList(array));
        TreeSet<String> ts = new TreeSet<>(list);

        for(String s : ts) {
            int count = 0;
            for(String ss : list) {
                if(s.equals(ss)) {
                    count++;
                }
            }
            System.out.println(s + " " + count);
        }
    }

#出力結果
x 3
y 4
z 3

#ポイント
・出力結果まで重複して表示させないように、コレクションのSetを使った。
・表示する順番をアルファベット順にしたかったので、TreeSetを使った。
・Arrays.asList()で、配列をリストとして返した。

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?