C# 合并数组、排序、删除重复项最优方案

本文介绍了一种高效的数组合并及去除重复项的方法,通过使用C#中的Dictionary数据结构实现,有效提升了处理大规模数据集的速度。

上一篇的解决办法显然不是最优的,先在找到了一种最优的解决办法,利用Directory进行合并数组,去除重复项。

代码如下:

效果如下:

 

6c08b189c14433890e244471.jpg


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


namespace DouHaoCount
{
class Program
{
static void Main( string [] args)
{
long T1 = DateTime.Now.Ticks;
Dictionary
< int , int > dic = new Dictionary < int , int > ();

int [] Arr1 = {58975个0 - 9的随机数};

int []Arr2 = {103991个0 - 9的随机数};

foreach ( int i in Arr1)
{
if ( ! dic.ContainsKey(i))
dic.Add(i, i);
}
foreach ( int i in Arr2)
{
if ( ! dic.ContainsKey(i))
dic.Add(i, i);
}

int [] Arr = new int [dic.Count];
int k = - 1 ;
foreach ( int item in dic.Keys)
{
k
++ ;
Arr[k]
= item;

}
Array.Sort(Arr);
long T2 = DateTime.Now.Ticks;
foreach ( int t in Arr)
{

Console.WriteLine(t);
}
Console.WriteLine((
double )(T2 - T1) / 10000.0 + " 毫秒,Arr1元素个数: " + Arr1.Length + " ,Arr2个数: " + Arr2.Length);
Console.ReadLine();
}
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值