c#一个list去掉其中重复元素

2023-08-24 13:37:56 浏览数 (1)

代码语言:javascript复制
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<int> list = new List<int>();
            list.Add(1);
            list.Add(2);
            list.Add(2);
            list.Add(3);
            list.Add(1);
            SameRemove(list);
        }
        public   static   void  SameRemove(List<int> list)   
        { 
           for  ( int  i  =   0 ; i  <  list.Count  -   1 ; i    )   
           {
               for (int j = list.Count - 1; j > i; j--)
               {
                   if (list[i] == list[j])
                   {
                       list.RemoveAt(j);
                   } 
                }
               
          }
           for (int i = 0; i < list.Count; i  )
           {
               Console.WriteLine(list[i]   " ");
           }
           Console.ReadLine();

        }
    }
}

0 人点赞