2011-10-13 131 views
0

假设我有一个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

你为什么要标记三种语言的无关你的问题?你的询问涉及哪一个? – mario

+0

你有这个标签为3种不同的语言? –

+0

这是功课吗? –

回答

2

您可以将数组中的所有值一起添加。如果它们等于数组的长度,或者它们等于0,则它们全部为1或全为0。

+0

数组值不一定是0和1。例如,它可能是12s和74s。关键是我想检查我的数组是否实际上包含2个不同的值(请参阅编辑) – user765368

+0

您可以通过将总和除以数组中的第一个数字来扩展我的答案。如果它等于数组的长度,它是一样的。零方案不会改变。 –

3

如果你想知道PHP中,你可以使用array_unique()探讨其独特的价值存在:

if (count(array_unique($array)) == 1) { 
    // It's either full of 1s or 0s. 
    // Then just probe the first entry. 
} 
0

在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解决方案时编写的。检查问题的编辑历史...

1

最简单的方法是只计算一个和零个数。例如(在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,如果所有元素都为零)

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."); 
} 

工作例如:http://jsfiddle.net/daNEH/

0

试试这个代码:

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; 
0
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/

0

另一种方法是使用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) 

所以可以测试所得阵列的长度。

0

我想你可以使用array_sum或array_filter函数。

0

我读过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"; 
    } 
} 
相关问题