「毎日Unity」の技術ブログ

開発で役立つ情報を発信する

【UnityC#】List内の重複要素を消去する方法

今回はList内の重複要素を消去する方法を記事にしました。

[ 重複を消す方法 ]

下記はListIntTestというint型のListから重複要素を消去し、コンソールに出力するスクリプトです。

using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public class ScriptTest : MonoBehaviour
{
    void Start()
    {
        List<int> ListIntTest = new List<int> { 1, 1, 2, 2, 2, 3, 3, 3, 3 };

        ListIntTest = ListIntTest.Distinct().ToList();

        for (int x = 0; x < ListIntTest.Count; x++)
        {
            Debug.Log(ListIntTest[x]);
        }
    }
}

コンソールには、1、2、3と出力されます。