C#不重复输出一个数组中所有元素的方法
作者:北风其凉 时间:2022-07-02 14:25:58
本文实例讲述了C#不重复输出一个数组中所有元素的方法。分享给大家供大家参考。具体如下:
1.算法描述
0)输入合法性校验
1)建立临时数组:与原数组元素一样。该步骤的目的是防止传入的原数组被破坏
2)对临时数组进行排序
3)统计临时数组共有多少个不同的数字。该步骤的目的是为了确定结果集数组的长度
4)建立结果集数组,只存放不同的数字
5)返回结果集
2.函数代码
/// <summary>
/// 建立包含原数组内所有元素且元素间互不重复的新数组
/// </summary>
/// <param name="array">原数组</param>
/// <param name="isAsc">true:升序排列/false:降序排列</param>
/// <returns>新数组</returns>
private static int[] DifferentElements(int[] array, bool isAsc = true)
{
//0.输入合法性校验
if (array == null || array.Length == 0)
{
return new int[] { };
}
//1.临时数组:与原数组元素一样
int[] tempArray = new int[array.Length];
for (int i = 0; i < tempArray.Length; i++)
{
tempArray[i] = array[i];
}
//2.对临时数组进行排序
int temp;
for (int i = 0; i < tempArray.Length; i++)
{
for (int j = i; j < tempArray.Length; j++)
{
if (isAsc)
{
if (tempArray[i] > tempArray[j])
{
temp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = temp;
}
}
else
{
if (tempArray[i] < tempArray[j])
{
temp = tempArray[i];
tempArray[i] = tempArray[j];
tempArray[j] = temp;
}
}
}
}
//3.统计临时数组共有多少个不同的数字
int counter = 1;
for (int i = 1; i < tempArray.Length; i++)
{
if (tempArray[i] != tempArray[i - 1])
{
counter++;
}
}
//4.建立结果集数组
int[] result = new int[counter];
int count = 0;
result[count] = tempArray[0];
for (int i = 1; i < tempArray.Length; i++)
{
if (tempArray[i] != tempArray[i - 1])
{
count++;
result[count] = tempArray[i];
}
}
//5.返回结果集
return result;
}
3.Main函数调用
static void Main(string[] args)
{
int[] array = new int[]
{
1, 9, 1, 9, 7, 2, 2, 5, 3, 4,
5, 6, 3, 3, 6, 2, 6, 7, 8, 0
};
//数组:包含原数组内全部元素且不重复(升序排列)
int[] result1 = DifferentElements(array);
foreach (int i in result1)
{
Console.Write(i.ToString() + "\t");
}
//数组:包含原数组内全部元素且不重复(降序排列)
int[] result2 = DifferentElements(array,false);
foreach (int i in result2)
{
Console.Write(i.ToString() + "\t");
}
Console.ReadLine();
}
4.程序输出示例
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,数组
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
C语言高效编程的几招小技巧
2023-11-02 14:12:50
java控制台输出图书馆管理系统
2022-06-13 01:29:29
![](https://img.aspxhome.com/file/2023/4/61284_0s.jpg)
Java(TM) Platform SE binary 打开jar文件的操作
2021-10-02 00:08:12
![](https://img.aspxhome.com/file/2023/4/62534_0s.jpg)
Java反射之通过反射获取一个对象的方法信息(实例代码)
2021-08-07 15:14:46
![](https://img.aspxhome.com/file/2023/2/66532_0s.jpg)
Spring Boot配置接口WebMvcConfigurer的实现
2023-11-27 23:28:50
![](https://img.aspxhome.com/file/2023/3/60523_0s.png)
openFeign服务之间调用保持请求头信息处理方式
2022-11-07 23:45:21
带你入门Java的泛型
2023-06-08 02:48:24
java实现微信扫码支付功能
2023-11-09 18:38:08
Java实现inputstream流的复制代码实例
2021-08-30 11:52:11
如何基于java实现Gauss消元法过程解析
2023-12-15 21:51:08
![](https://img.aspxhome.com/file/2023/3/66953_0s.png)
Spring整合Dubbo框架过程及原理解析
2022-01-31 01:49:16
![](https://img.aspxhome.com/file/2023/1/66221_0s.png)
详解java 客户端链接不上redis解决方案
2023-11-12 10:12:15
![](https://img.aspxhome.com/file/2023/4/59204_0s.png)
Java后台线程操作示例【守护线程】
2023-11-25 01:35:44
Java如何解决发送Post请求报Stream closed问题
2021-12-12 04:20:10
![](https://img.aspxhome.com/file/2023/6/64956_0s.jpg)
使用chatgpt实现微信聊天小程序的代码示例
2022-04-26 17:18:24
![](https://img.aspxhome.com/file/2023/0/67500_0s.jpg)
C#中应用程序集的装载过程详解
2023-03-30 19:06:05
![](https://img.aspxhome.com/file/2023/5/68065_0s.png)
Java中Servlet的生命周期详解
2023-09-10 03:57:59
![](https://img.aspxhome.com/file/2023/7/61057_0s.png)
Java实现去除文档阴影的示例代码
2023-08-31 11:45:48
![](https://img.aspxhome.com/file/2023/5/58415_0s.png)
java仿windows记事本小程序
2023-11-25 09:24:43
Spring Security 控制授权的方法
2023-08-06 19:21:08
![](https://img.aspxhome.com/file/2023/4/58024_0s.png)