C#不重复输出一个数组中所有元素的方法

感谢原作者:https://www.xp.cn/b.php/53165.html

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();
}
C#查找数组中相同的元素输出它们的索引,可以通过以下步骤实现: 1. 创建一个新的数据结构来存储元素及其出现的索引。可以使用一个`Dictionary<int, List<int>>`,其键是数组中元素,值是一个包含该元素所有索引的列表。 2. 遍历数组,对于数组中的每个元素,检查它是否已经在字典有记录: - 如果元素已经存在于字典的键,那么将当前索引添加到该键对应的列表。 - 如果元素不在字典的键,向字典添加一个新的键值对,键是该元素,值是一个新的列表,包含当前元素的索引。 3. 最后,遍历字典,并输出每个键值对,即每个元素及其所有出现的索引。 下面是一个简单的代码示例: ```csharp using System; using System.Collections.Generic; public class Program { public static void Main() { int[] array = { 1, 3, 2, 4, 2, 1, 3, 1 }; Dictionary<int, List<int>> elementIndices = new Dictionary<int, List<int>>(); // 遍历数组,查找元素并记录索引 for (int i = 0; i < array.Length; i++) { if (elementIndices.ContainsKey(array[i])) { elementIndices[array[i]].Add(i); } else { elementIndices[array[i]] = new List<int> { i }; } } // 输出元素及其索引 foreach (var pair in elementIndices) { if (pair.Value.Count > 1) { Console.WriteLine($"元素 {pair.Key} 出现了多次,索引为:{string.Join(", ", pair.Value)}"); } } } } ``` 这段代码会输出数组中重复出现的元素及其索引。注意,如果数组中元素都是唯一的,那么字典只会包含每个元素及其第一次出现的索引。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值