这是一个采访问题:
从整数数组中返回第一个重复元素的最佳方法是什么?
示例:
给定数组[12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98]
。
在这种情况下,返回值为12
。
如何做到这一点?
答案 0 :(得分:11)
我认为如果你看一下性能,foreach循环就是faster
# temp array
$array_help = array();
# run over the array
foreach ($array as $val) {
if (isset($array_help[$val]))
# found if is set already !
return $val;
else
# its the first time this value appear
$array_help[$val] = 1;
}
答案 1 :(得分:9)
这将为您提供所有重复值及其原始位置:
$diff = array_diff_assoc($array, array_unique($array));
var_dump($diff);
结果:
array(3) {
[4]=> int(12)
[9]=> int(83)
[14]=> int(98)
}
答案 2 :(得分:3)
function findDuplicate ($array) {
while (($item = array_shift($array)) !== null) {
if (in_array($item, $array)) return $item;
}
}
答案 3 :(得分:3)
您可以使用array_unique
删除所有重复值,然后遍历原始数据和结果数组,并返回未在结果数组中出现的第一个值。像这样:
$arr = array(12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
function first_dupe($arr) {
$res = array_unique($arr);
foreach ($arr as $key => $val) {
if ($res[$key] !== $val)
return $val;
}
}
echo first_dupe($arr);
答案 4 :(得分:2)
$arrs = array(12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
// or a associate array like as $arrs = array('a'=>12, 'b'=>46, 'c'=>244, 'd'=>0, 'e'=>12, 'f'=>83, 'g'=>48, 'h'=>98, 'i'=>233, 'j'=>83, 'k'=>26, 'l'=>91, 'm'=>119, 'n'=>148, 'o'=>98);
$data = "12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98";
static $repeat = '';
foreach($arrs as $arr){
if (substr_count($data,$arr)>1) {
$repeat = $arr;
break;
}
}
if ($repeat) echo 'The first repeated element out of the array of integers is '.$repeat;
答案 5 :(得分:1)
$testData = array(46, 12, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
function repeated($val) {
return $val > 1;
}
$firstRepeatedElement = array_shift(array_keys(array_filter(array_count_values($testData),'repeated')));
答案 6 :(得分:0)
function getFirstRepetition($array) {
$prev = array();
foreach($array as $value) {
if (in_array($value, $prev)) {
return $value;
}
$prev[] = $value;
}
return FALSE;
}
答案 7 :(得分:0)
使用递归的无环路解决方案。我认为这将是最快的,但需要更多的记忆。快速因为我们继续前进因此阵列的大小继续减少因此cpu上的工作量减少。
$data = array(46, 12, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
function find(&$input)
{
$current = array_shift($input);
if(in_array($current,$input))
{
return $current;
}
return find($input);
}
echo find($data);
答案 8 :(得分:0)
$arrs = array(12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
static $repeat = '';
foreach($arrs as $arr) {
if ((count(array_keys($arrs,$arr))) > 1) {
$repeat = $arr;
break;
}
}
if ($repeat) echo 'The first repeated element out of the array of integers is '.$repeat;
//This solution is working for associate array too, for example:
// $arrs = array('a'=>12, 'b'=>46, 'c'=>244, 'd'=>0, 'e'=>12, 'f'=>83, 'g'=>48, 'h'=>98, 'i'=>233, 'j'=>83, 'k'=>26, 'l'=>91, 'm'=>119, 'n'=>148, 'o'=>98);
答案 9 :(得分:0)
$array = array(12, 46, 46, 0, 18, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
$count_array = array_count_values($array);
foreach($count_array as $key=>$value)
{
if($value>=2)
{
echo $key;
break;
}
}
答案 10 :(得分:0)
$arr = array(12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
$arrHelp = array();
foreach($arr as $int) {
if (in_array($int, $arrHelp)) {
echo $int;
break;
}
else {
array_push($arrHelp, $int);
}
}
答案 11 :(得分:0)
here N is size of array and A is array,
step1:add N to index of the value
step2:traverse the array and find the first number which has frequency
greater than 1 break there.
O(n) with O(1) size complexity!
for(int i=0;i<N;++i)
A[A[i]%N]+=N;
int val,freq;
for(int i=0;i<N;++i)
{
val=A[i]%N;
freq=A[val]%N;
if(freq>1)
{
cout<<val;
break;
}
}
答案 12 :(得分:-1)
或者您可以使用array_count_values()函数来获取它。
$arr = array(12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98);
$freq_arr = array_count_values($arr);
echo $freq_arr[0];
请参阅PHP手册上的here。