Home > Back-end >  How to determine if all the elements in an array of values are consistent
How to determine if all the elements in an array of values are consistent

Time:09-22

 int [] pacgagecount=userService. SetUsersCharge (cardnumber, pacgageid, httpSession); 
For (int I: pacgagecount) {
For (int j: pacgagecount) {
If (pacgagecount [I]==pacgagecount [j]) {
return true;
}
}

}
return false;



I always feel I wrote this for loop is very strange,
Debug, felt only judge pacgagecount [0]==pacgagecount [0] and then return true

CodePudding user response:

Change is not equal to, can return false ah, as long as there is a false unequal

CodePudding user response:

This is not necessary to get double loop

CodePudding user response:

The Arrays. Sort (arr);
Return arr [0]==arr (arr. Length - 1),
Can get this done in 2 rows, not so why trouble

CodePudding user response:

In fact, the first line didn't work, the straight line

CodePudding user response:

I was wrong, you have to 2 rows

CodePudding user response:

Stream of (pacgagecount). Distinct (). The count () & gt; 1

CodePudding user response:

Pacgagecount [0]==pacgagecount [0], this ratio is address, use the equals a try,

CodePudding user response:

Global ordering cost is too big?
Dichotomy, array is divided into two and a half, each half is the first and the last one, if there is a difference between straight out of the, no difference, split into half half again, repeat this process is
O (logN) time after system

CodePudding user response:


 
Int a=pacgagecount [0].
For (int I: pacgagecount) {
If (I. {
=a)return false;
}
}
return true;

Similar to bubble, directly traverse again one comparison, one O (n) not xiang yao
7th floor that additionally, int directly comparing the==
The eighth floor of the time complexity is O (n), and each is divided into two and a half have to compare the value of the first or the last two and a half

CodePudding user response:

 for (int I=0; iFor (int j=I + 1; jIf (a [I]!=a, [j]) return false.//if there is a different means inconsistent 
}
}
return true;//here means that you could walk all consistent

CodePudding user response:

Fix, no dual loop, a heavy cycle, such as 9 l code
  • Related