C# 如何在数组中找到重复的相邻对
我想写一个简单易用的方法,让我在数组中找到一对重复项,并显示该对存在的索引号 到目前为止,我只需要处理方法头和一个输出示例C# 如何在数组中找到重复的相邻对,c#,arrays,algorithm,linq,duplicates,C#,Arrays,Algorithm,Linq,Duplicates,我想写一个简单易用的方法,让我在数组中找到一对重复项,并显示该对存在的索引号 到目前为止,我只需要处理方法头和一个输出示例 int Duplicates (int[] testArray){ int[] testArray = {1,5,6,8,9,4,4,6,3,2}; } 我唯一希望返回的是相邻对的索引位置,在本例中为5,即(4,4)。如果没有相邻的对,我也希望能够打印“未找到重复对” 有人能帮我开始吗,因为我不知道人们会如何开始做这样的事情。intpreviousvalue=-1//将
int Duplicates (int[] testArray){
int[] testArray = {1,5,6,8,9,4,4,6,3,2};
}
我唯一希望返回的是相邻对的索引位置,在本例中为5,即(4,4)。如果没有相邻的对,我也希望能够打印“未找到重复对”
有人能帮我开始吗,因为我不知道人们会如何开始做这样的事情。intpreviousvalue=-1//将其设置为您不期望的内容
int previousValue = -1; //set it to something you're not expecting
for (int i=0; i <testArray.Count; i++) {
int currentValue = testArray[i];
if (currentValue.equals(previousValue) {
//we have a duplicate
duplicateList.add(i); //for the position of the duplicate
}
previousValue = currentValue;
}
if (duplicateList.Count == 0) {
//no duplicates found
} else {
return duplicateList.toArray();
}
对于(int i=0;i这很简单,当你分解问题时,你必须查看每个元素,然后将其与下一个元素进行比较。唯一主要的问题是,如果你将最后一个元素的索引与索引+1进行比较,你将耗尽数组,这将导致数组越界异常,这就是我们检查位置的原因
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Misc
{
class Program
{
static int duplicates(int[] array)
{
for (int i = 0; i < array.Length-1; i++)
{
if (array[i] == array[i+1])
{
return i;
}
}
return -1;
}
static void Main(string[] args)
{
int[] testArray = { 1, 5, 6, 8, 9, 4, 4, 6, 3, 2 };
Console.WriteLine(duplicates(testArray));
Console.ReadKey(); // block
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
命名空间杂项
{
班级计划
{
静态整数重复(int[]数组)
{
for(int i=0;i
尝试以下Linq查询
编辑
以下是用于索引重复项的LINQ查询
var adjacentIndex = testArray
.Skip(1)
.Select((value,index) => value == testArray[index] ? index : -1)
.Where (x=> x!= -1);
在这个LINQ查询中,我能想到的唯一缺点是它使用-1作为丢弃的值。对于索引,它总是为true,但我通常不建议这样做。它所做的是检查数组的下一个元素是否与当前元素相同,如果为true,则返回当前索引,否则返回-1,然后只选择大于t的索引韩零
int[] testArray = {1, 5, 6, 8, 9, 4, 4, 6, 3, 2, 2};
var duplicateIndexes = testArray.
Select((value, index) => testArray.Length > index + 1 &&
testArray[index + 1] == value ? index : -1).
Where(index => index > 0).
ToArray();
你熟悉for/while循环的概念吗?你试过使用其中一个吗?你能说明你最初的方法是什么,并解释它是如何失败的吗?如果你在问题中提供更多关于这些的细节,你可能会得到更适合你的答案-1.代码,而不尝试解释如何接近它-给那些看起来缺乏对for循环的理解。如果您在解决方案中添加文本解释,我将删除-1。您的代码也缺少缩进。在最后添加解释,将修复问题。@Haedrian,您不应该回答OP自己甚至没有尝试解决它的问题。这样更好(虽然可以通过更简单的代码来完成,但这是一个意见问题)。我删除了-1。为什么要存储以前的值?开始在索引1
处循环,并使用testArray[I]==testArray[I-1]
测试重复项。为什么if(I==array.Length)继续;
?代码甚至不会达到该点。这样更容易摸索。还要注意:在两种情况下将返回0:(1)无重复[0,1,2]。(2):在第一个元素[0,0,1]中重复{1,5,6,8,9,4,2,6,3,2};
将抛出异常IndexOutOfRange
这将只返回第一个副本的索引!将方法的返回类型更改为IEnumerable
,在循环中使用yield return i;
并删除最后的return-1;
。当然,value==testArray[index]
将始终为真。否,由于跳过(1)
语句,索引被移动1。请参阅@ashburaczenko,您似乎错过了跳过(1)
part。公平地说,这个问题是问对的索引,而不是对本身的索引。@IchabodClay,我先错过了它。我还更新了索引的答案。Eve,你的语法非常聪明。谢谢你的语法。从上述语法中删除testArray.Length>index+1部分显示错误索引超出了数组的界限。w为什么会这样?
int[] testArray = {1, 5, 6, 8, 9, 4, 4, 6, 3, 2, 2};
var duplicateIndexes = testArray.
Select((value, index) => testArray.Length > index + 1 &&
testArray[index + 1] == value ? index : -1).
Where(index => index > 0).
ToArray();