我有二维数组,如:
{2 , 6 , 46, 8 , 7 , 25, 64 , 9 , 10},
{6 , 10, 50, 12, 11, 29, 68 , 13, 14},
{46, 50, 90, 52, 51, 69, 108, 53, 54}
如何找到重复的元素,例如'6', '46' and '50'
?
我的代码找到连续的重复项:
for (int i = 0; i < a2.length; i++) {
for (int j = 0; j < a2[i].length; j++) {
cursor = a2[i][j];
if(j + 1 < a2[i].length){
if(cursor == a2[i][j + 1]){
System.out.println(cursor + "has duplicate in this array");
}
}
}
}
答案 0 :(得分:0)
迭代所有元素并将其保存在临时集中 当您遇到重复时,列表将包含它。
$Timeout=10000000;
$fileName = "0.iso";
$data = "C:\\$fileName";
$url = "http://nexus.lab.local:8081/nexus/content/sites/myproj/$fileName";
#$buffer = [System.IO.File]::Open("$data",[System.IO.Filemode]::Open, [System.IO.FileAccess]::Read) #Err Cannot convert argument "buffer", with value: "System.IO.FileStream", for "Write" to type "System.Byte[]":
#$buffer = gc -en byte $data # too much space in memory
$buffer = [System.IO.File]::ReadAllBytes($data) #Limit 2gb
[System.Net.HttpWebRequest] $webRequest = [System.Net.WebRequest]::Create($url)
$webRequest.Timeout = $timeout
$webRequest.Method = "POST"
$webRequest.ContentType = "application/data"
#$webRequest.ContentLength = $buffer.Length;
$webRequest.Credentials = New-Object System.Net.NetworkCredential("admin", "admin123");
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($buffer, 0, $buffer.Length)
$requestStream.Flush()
$requestStream.Close()
[System.Net.HttpWebResponse] $webResponse = $webRequest.GetResponse()
$streamReader = New-Object System.IO.StreamReader($webResponse.GetResponseStream())
$result = $streamReader.ReadToEnd()
return $result
$stream.Close()
输出:
[50,6,10,46]
答案 1 :(得分:0)
试试这段代码: -
import java.util.Arrays;
import java.util.List;
public class ArrayTest {
public static void main(String[] args) {
Integer[][] myarray = new Integer[][]{
{ 10, 20, 30, 40 },
{ 50, 77, 60, 70 },
{ 33, 22, 88, 99 },
{ 21, 66, 65, 21 }
};
int i,j;
for(i=0;i<myarray.length;i++)
{
for(j=0;j<myarray.length;j++)
{
int temp= myarray[i][j];
myarray[i][j]=0;
List<Integer> rowvalues = Arrays.asList(Arrays.asList(myarray).get(i));
Boolean b=rowvalues.contains(temp) ;
if(b==true)
{
System.out.println("duplicate at ["+i+"]["+j+"] is: "+temp);
}
myarray[i][j]=temp;
}
}
}
}