假設我有一個php數組,可以是全1,全2或全1和2。例如,我可以有array(1, 1, 1, 1, 1, 1)
或array(2, 2, 2, 2, 2, 2)
或array(2, 2, 1, 1, 2, 1)
。檢查一個php數組中是否存在兩個不同的值
如何檢查我的數組是否實際上是全1,全2的數組,或者我的數組是否實際上包含1和2?
假設我有一個php數組,可以是全1,全2或全1和2。例如,我可以有array(1, 1, 1, 1, 1, 1)
或array(2, 2, 2, 2, 2, 2)
或array(2, 2, 1, 1, 2, 1)
。檢查一個php數組中是否存在兩個不同的值
如何檢查我的數組是否實際上是全1,全2的數組,或者我的數組是否實際上包含1和2?
您可以將數組中的所有值一起添加。如果它們等於數組的長度,或者它們等於0,則它們全部爲1或全爲0。
數組值不一定是0和1。例如,它可能是12s和74s。關鍵是我想檢查我的數組是否實際上包含2個不同的值(請參閱編輯) – user765368
您可以通過將總和除以數組中的第一個數字來擴展我的答案。如果它等於數組的長度,它是一樣的。零方案不會改變。 –
如果你想知道PHP中,你可以使用array_unique()
探討其獨特的價值存在:
if (count(array_unique($array)) == 1) {
// It's either full of 1s or 0s.
// Then just probe the first entry.
}
在Java ...
public static void allTheSame(int[] array) {
for (int i = 1; i < array.length; i++) {
if (array[i] != array[i - 1]) {
return false;
}
}
return true;
}
該算法可以轉錄成其他列出的語言,雖然他們可能是一些乾淨的方式來做到這一點。 (但要注意整潔解決方案的效率......如果這對您的應用程序很重要)。
請注意,此方法將提供false
結果,比包含整理或求和數組元素的任何整潔解決方案更快,並且它不會假定元素值是什麼。
注意:這個答案是在標記表明OP想要Java,Javascript和PHP解決方案時編寫的。檢查問題的編輯歷史...
最簡單的方法是隻計算一個和零個數。例如(在python):
ones = zeroes = 0;
for i in range(len(my_array)):
if my_array[i] == 1: ones = ones + 1
else zeroes = zeroes + 1
也可以乘以每個元件一起(1,如果所有的)以及陣列中添加的每個元件(0,如果所有元素都爲零)
可以做到這一點用簡單的if-statement
。下面是一些JavaScript:
if (myArray.indexOf(1) > -1) {
// there are 1s, are there 0s?
if (myArray.indexOf(0) > -1) {
console.log("1s and 0!");
} else {
console.log("Only 1s.");
}
} else {
console.log("Only 0s.");
}
試試這個代碼:
int[] intArray = new int[5];
boolean hasZero, hasOne, hasBoth;
for(int integer : intArray)
{
switch(integer)
{
case 0:
hasZero = true;
break;
case 1:
hasOne = true;
break;
}
}
hasBoth = hasZero && hasOne;
function allElementsEqual(array){
var start = array[0],
same = true;
for(i = 1;i < array.length;i++){
same &= (start === array[1]);
}
return same;
}
這個功能應該做的工作細http://jsfiddle.net/WNxg4/
另一種方法是使用array_diff
,前提是你只有兩個不同的nu mbers。只需將數字的乾草堆與單個數字進行比較(選擇乾草堆中的一個)。
例如:
$haystack_mixed = array(2,2,2,1,1);
$haystack_1 = array(1,1,1,1);
$haystack_2 = array(2,2,2,2);
print_r(array_diff($haystack_mixed, array(1)));
// The result is not the same as the $haystack since there are 1's in it.
// Array ([0] => 2 [1] => 2 [2] => 2)
print_r(array_diff($haystack_1, array(1)));
// This one is made up of all 1's
// Array ()
print_r(array_diff($haystack_2, array(1)));
// This one is made up of all 2's (same length as $haystack_2)
// Array ([0] => 2 [1] => 2 [2] => 2 [3] => 2)
所以可以測試所得陣列的長度。
我想你可以使用array_sum或array_filter函數。
我讀過9個答案,他們都很漂亮,我想只是用最簡單的方式去做。
is_mixed($array){
$count = count($array);
//we go trough every element in the array
for($i=1;$i<$count;$i++){
//if the element n is distinct from the n-1
//then return true (is_mixed)
if ($array[$i] != $array[$i-1]) return true;
}
//if it didn't return anything yet, it means
//all the elements are the same. Then, just
//return the first one, as they're all the same
// (either 1 or 2)
return $array[0];
}
這第二個其實我最喜歡的:
function what_they_are($array){
$total = array_sum($array);
$count = count($array);
if ($total == 0) {
return "they're all 0";
}else if ($total/$count == 2){
return "they're all 2";
}else if ($total == $count){
return "they're all 1";
}else{
return "they're mixed";
}
}
你爲什麼要標記三種語言的無關你的問題?你的詢問涉及哪一個? – mario
你有這個標籤爲3種不同的語言? –
這是功課嗎? –