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

C# 如何在c中显示数组中重复的元素位置#,c#,arrays,C#,Arrays,如何在c#控制台中以数组的形式显示重复的元素及其索引位置 我有这个问题 用简单的c# 用户给了我5个数字并存储在一个数组中,现在我给了用户一个选项,可以根据其位置检查这些数字 现在的问题是,如果用户先前存储的5个数字中有2个或更多相同的数字。如何显示这两个数字及其位置 using System; namespace ConsoleApplication3 { class Program { static void Main(string[] args)

如何在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();
        }
    }
}
使用系统;
命名空间控制台应用程序3
{
班级计划
{
静态void Main(字符串[]参数)
{
int[]arr=新的int[5];
整数检查=0;
int位置=0;
Console.WriteLine(“输入5个元素”);
对于(int i=0;i<5;i++)
{
arr[i]=Convert.ToInt32(Console.ReadLine());
}
int值;
Console.WriteLine(“输入no进行搜索”);
value=Convert.ToInt32(Console.ReadLine());
对于(int i=0;i<5;i++)
{
如果(值==arr[i])
{
{
检查=1;
位置=i;
}
}
}
如果(检查==1)
{
Console.WriteLine(“在位置“+位置找到编号”);
}
其他的
{
控制台。写入线(“未找到”);
}
Console.ReadKey();
}
}
}
请帮帮我

如果用户输入的数字为1 2 3 1 2 它们被储存起来 然后当用户搜索nos时 应显示在第1和第2位置的编号??
帮助

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

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.");
bool check=false;
对于(int i=0;i<5;i++)
{
如果(值==arr[i])
{
检查=正确;
WriteLine(“在位置{0}处找到编号”,i);
}
}
如果(!check)Console.WriteLine(“根本找不到编号”);
或者将索引保存在列表中,然后输出:

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.");
List foundindex=new List();
对于(int i=0;i<5;i++)
{
如果(值==arr[i])
12.添加(i);
}
如果(FoundIndexes.Count>0)
foreach(foundindex中的int索引)
WriteLine(“在{0}处找到的编号”,索引);
其他的
Console.WriteLine(“根本找不到号码”);

为了找到重复,您可以尝试使用Linq:

//用于跟踪找到的号码的位置
var currentPosition=0;
//保留索引
var foundIndexes=新列表();
//一些虚拟数据
int[]数字={1,2,3,5,2,3,3,5,7};
对于(变量i=0;i
{
Console.WriteLine(“在位置“+索引处找到编号”);
});

下面是另一个使用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可能会让人望而生畏,特别是如果你是新手的话——如果这个解决方案有帮助的话,我可以进一步阐述它的工作原理。

嘿,兄弟,请告诉我一些更简单的方法,只使用系统,就像我写的那样,不使用var或bool作为任务
//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);
});
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));