2012-12-30 27 views
-1

我期待寫一個簡單和容易的方法,讓我找到一個數組一對重複的,並顯示其中的一對將存在的索引號重複對相鄰。如何找到在一個數組

到目前爲止我只有方法頭一起工作並輸出

int Duplicates (int[] testArray){ 

int[] testArray = {1,5,6,8,9,4,4,6,3,2}; 
} 

我想要返回的唯一事情是相鄰的對索引位置的一個例子,即5在這種情況下這將是(4,4)。如果沒有相鄰對我也想能夠打印「沒有重複對發現」

誰能幫助我不知道怎麼一會甚至開始對這樣的工作,我上手。

+8

你熟悉的概念/ while循環?你嘗試過使用其中之一嗎?你能說明你最初的方法是什麼,並解釋它是如何失敗的?如果你在你的問題中提供了更多的細節,你很可能會得到更適合你的答案 – amit

回答

3

嘗試以下Linq查詢Demo here

int[] testArray = {1,5,6,8,9,4,4,6,3,2}; 

var adjacentDuplicate = testArray 
    .Skip(1) 
    .Where((value,index) => value == testArray[index]) 
    .Distinct(); 

if (adjacentDuplicate.Any()) 
{  
    // Print adjacentDuplicate 
} 
else 
{ 
    // No duplicates found. 
} 

編輯

以下是LINQ查詢重複的指標。

var adjacentIndex = testArray 
    .Skip(1) 
    .Select((value,index) => value == testArray[index] ? index : -1) 
    .Where (x=> x!= -1); 
+2

當然,'value == testArray [index]'將始終爲真。 –

+0

不,索引由於Skip(1)語句而被移位1。參見[這裏](http://ideone.com/y6jcGJ) – Tilak

+0

@AshBurlaczenko看來你錯過了'跳過(1)'一部分。 –

-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(); 
} 

解釋 - 我們將通過一次一個地檢查它們來解決這個問題。

for循環,直到它穿過整個陣列的每個時間將增加i的值由一個。

在每一步中,當前值將與先前的值一起檢查。如果它們相同,則將此位置添加到輸出中。然後前一個值變成最後的當前值,循環繼續。

+2

-1。代碼沒有任何解釋如何處理它的嘗試 - 對似乎缺乏對for循環的理解的人。如果您在解決方案中添加文本解釋,我將刪除-1。你的代碼也缺乏縮進。 – amit

+0

在最後添加了解釋,將修正意圖。 – Haedrian

+0

@海德里安,你不應該回答OP甚至沒有試圖解決它自己的問題。 –

0

它,當你打破下來的問題很簡單,你要看看每個元素,然後把它比作下一個。唯一的主要疑難雜症的是,你會耗盡陣如果你比較最後一個元素的索引+ 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 
     } 
    } 
} 
+2

爲什麼'if(i == array.Length)繼續;'?該代碼甚至不會達到這一點。 –

+0

這樣更容易理解。 –

+0

另請注意:0將返回兩種情況:(1)沒有重複[0,1,2]。 (2):在欺騙第一元素[0,0,1] – amit

1

我在這個LINQ查詢中唯一的缺點是它使用-1作爲丟棄值。在索引的情況下,它總是如此,但我通常不會推薦這樣做。它所做的是檢查數組的下一個元素是否與當前的元素相同,如果爲true,則返回當前索引,否則返回-1,然後僅選擇大於零的索引。

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(); 
+0

Eve,你的語法非常聰明的語法。感謝你的syntax.remove testArray.Length> index + 1部分從上面的語法show me error索引超出了數組的界限。爲什麼會發生這種情況? – shamim