C#删除数组重复元素并输出

来源:互联网 发布:java异常日志记录 编辑:程序博客网 时间:2024/05/21 07:13

今天第一天开通了博客,碰巧在学习算法,就写一篇练练手,有问题请指出我会改正大笑


using System;

using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace 第一章练习题
{
    class Program
    {
        /// <summary>
        /// 删除数组中重复的元素
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            int count = 0;
            int[] zz = new int[] { 1, 2, 3, 4, 1, 9, 8, 3 };
            for (int i = 0; i < zz.Length; i++)
                for (int j = i+1; j < zz.Length; j++)
                {
                    if (zz[i] == zz[j])
                    {
                        
                        int temp = j;
                        for(int z=temp+1;z<zz.Length;z++)
                        {
                            zz[z-1] = zz[z];
                          
                        }
                        count++;
                    }
                }
            for (int i = 0; i < zz.Length-count/2; i++)
                Console.WriteLine(zz[i]);


        }


    }
}
原创粉丝点击