如何在 C# 中显示数组中的重复元素位置

本文关键字:元素 位置 显示 数组 | 更新日期: 2023-09-27 18:32:51

如何在 C# 控制台中显示数组中的重复元素及其索引位置

我有这个问题在简单的 C# 中用户给了我 5 个数字并存储在一个数组中,现在我为用户提供了检查数字相对于其位置的选项

现在的问题是,如果用户之前存储的 5 个号码中有 2 个或更多相同的号码。如何显示两者及其位置?

using System;
namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int[] arr = new int[5];
            int check = 0;
            int position = 0;
            Console.WriteLine("Enter 5 elements");
            for (int i = 0; i < 5; i++)
            {
                arr[i] = Convert.ToInt32(Console.ReadLine());
            }
            int value;
            Console.WriteLine("Enter no to search");
            value = Convert.ToInt32(Console.ReadLine());
            for (int i = 0; i < 5; i++)
            {
                if (value==arr[i])
                {
                    {
                        check = 1;
                        position = i;
                    }
                }
            }
            if (check == 1)
            {
                Console.WriteLine("number is found At position " +position);
            }
            else
            {
                Console.WriteLine("not found");
            }
            Console.ReadKey();
        }
    }
}

请帮助我

如果用户输入的数字为 1 2 3 1 2并存储它们然后当用户搜索 nos 时它应该显示数字在第一个和第二个位置找到??help

如何在 C# 中显示数组中的重复元素位置

您可以在检查循环中输出找到的数字的索引,如下所示:

bool check = false;
for (int i = 0; i < 5; i++)
{
    if (value==arr[i])
    {
        check = true;
        Console.WriteLine("number is found At position {0}", i);
     }
}
if (!check) Console.WriteLine("Number not found at all.");

或者,您将索引保存在列表中,然后输出:

List<int> foundIndices = new List<int>();
for (int i = 0; i < 5; i++)
{
    if (value==arr[i])
        foundIndices.Add(i);
}
if (foundIndices.Count > 0)
    foreach(int index in foundIndices)
        Console.WriteLine("Number found at {0}", index);
else
    Console.WriteLine("Number not found at all.");

为了找到重复项,你可以尝试使用Linq

  int[] source = { 1, 2, 3, 4, 3, 5, 1, 1};
  var result = source.Select((item, index) => new {
    item = item,
    index = index
  })
  .GroupBy(v => v.item, v => v)
  .Where(chunk => chunk.Count() >= 2)
  .OrderBy(chunk => chunk.Key)
  .Select(chunk => String.Format("item {0} appears at {1} positions", 
     chunk.Key, 
     String.Join(", ", chunk.Select(v => v.index))));
  String report = String.Join(Environment.NewLine, result);
  // item 1 appears at 0, 6, 7 positions
  // item 3 appears at 2, 4 positions
  Console.Write(report);
//Used to track position of found numbers
var currentPosition = 0;
//Keeps the indexes 
var foundIndexes = new List<int>();
//Some dummy data
int[] numbers = {1, 2, 3, 5, 2, 3, 3, 5, 7};
for (var i = 0; i < numbers.Length; i++)
    {
       if (numbers[i] == value)
       {
           foundIndexes.Add(i);
       }
    }
//can use the LINQ for each if you like it better (I do)
foundIndexes.ForEach(index =>
{
     Console.WriteLine("number is found At position " + index);
});

下面是如何使用 LINQ 执行此操作的另一个示例:

var appearsMoreThanOnce = arr
    .Distinct()
    .ToDictionary(k => k, v => Enumerable.Range(1, arr.Length)
        .Where(i => arr[i-1] == v))
    .Where(kvp => kvp.Value.Count() >= 2);
foreach (var number in appearsMoreThanOnce)
    Console.WriteLine(number.Key + " appears at: " + string.Join(",", number.Value));

LINQ 可能非常令人生畏,特别是如果您是新手 - 如果有帮助,我可以进一步详细说明此解决方案的工作原理。