给定一个数组,判定该数组中是否有重复元素。
判定该数组中是否有重复元素总结出以下实现方案:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace Demo{class Program{/*** 判定一个字符串中是否有重复的元素。*/static void Main(string[] args){string[] arr = new string[] { "1", "2", "3", "4", "3" };bool isContainsSameItem = IsSameWithForeach(arr);Console.WriteLine(isContainsSameItem);isContainsSameItem = IsSameWithSortAndCompare(arr);Console.WriteLine(isContainsSameItem);isContainsSameItem = IsSameWithHashSet(arr);Console.WriteLine(isContainsSameItem);isContainsSameItem = IsSameWithArrayContains(arr);Console.WriteLine(isContainsSameItem);Console.ReadKey();}/*** 利用array.contains存储及判断是否存在重复数据* **/static bool IsSameWithArrayContains(string[] arr){var newArr = new string[arr.Length];var idx = 0;foreach (var i in arr){if (false == newArr.Contains(i)){newArr[idx] = i;idx++;}else{return true;}}return false;}/*** 利用hasset的原理来实现* **/static bool IsSameWithHashSet(string[] arr){ISet<string> set = new HashSet<string>();for (var i = 0; i < arr.Length; i++){// 这里可利用该元素来实现统计重复的原理有哪些,及重复个数。//bool state = set.Add(arr[i]); // 如果返回false,表示set中已经有该元素。//Console.WriteLine(state);set.Add(arr[i]);}return set.Count != arr.Length;}/***排序后,比较相邻的数据是否有重复的。*/static bool IsSameWithSortAndCompare(string[] arr){// 先排序 Array.Sort(arr);//Console.WriteLine(string.Join(",", arr));// 对比相邻的数据是否相同for (var i = 0; i < arr.Length - 1; i++){if (arr[i] == arr[i + 1])return true;}return false;}/*** 双层遍历*/static bool IsSameWithForeach(string[] arr){for (var i = 0; i < arr.Length - 1; i++){for (var j = i + 1; j < arr.Length; j++){if (arr[i] == arr[j])return true;}}return false;}}}
其中我个人觉得是:IsSameWithHashSet(string[] arr)效率最高。
测试性能,测试代码:
1 string[] arr = new string[100000]; 2 // 给arr填充为一个没有任何重复数据的数组来,测试性能。 3 for (var i = 0; i < arr.Length; i++) 4 { 5 arr[i] = i.ToString(); 6 } 7 8 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); 9 watch.Start();10 bool isContainsSameItem = IsSameWithForeach(arr);11 Console.WriteLine(isContainsSameItem);12 watch.Stop();13 Console.WriteLine("IsSameWithForeach 耗时:" + watch.ElapsedMilliseconds + "milliseconds");14 15 watch = new System.Diagnostics.Stopwatch();16 watch.Start();17 isContainsSameItem = IsSameWithSortAndCompare(arr);18 Console.WriteLine(isContainsSameItem);19 watch.Stop();20 Console.WriteLine("IsSameWithSortAndCompare 耗时:" + watch.ElapsedMilliseconds + "milliseconds");21 22 watch = new System.Diagnostics.Stopwatch();23 watch.Start();24 isContainsSameItem = IsSameWithHashSet(arr);25 Console.WriteLine(isContainsSameItem);26 watch.Stop();27 Console.WriteLine("IsSameWithHashSet 耗时:" + watch.ElapsedMilliseconds + "milliseconds");28 29 watch = new System.Diagnostics.Stopwatch();30 watch.Start();31 isContainsSameItem = IsSameWithArrayContains(arr);32 Console.WriteLine(isContainsSameItem);33 watch.Stop();34 Console.WriteLine("IsSameWithArrayContains 耗时:" + watch.ElapsedMilliseconds + "milliseconds");35 36 Console.ReadKey();
测试效果:
如果觉得《c#:判断一个数组元素中否有重复元素》对你有帮助,请点赞、收藏,并留下你的观点哦!