I was just going through these questions for PHP and got stuck at one of them. The question is:
You have a PHP 1 dimensional array. Please write a PHP function that
takes 1 array as its parameter and returns an array. The function must
delete values in the input array that shows up 3 times or more?
For example, if you give the function
array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9)the function will returnarray(1, 3, 5, 2, 3, 1, 9)
I was able to check if they are repeating themselves but I apply it to the array I am getting as input.
function removeDuplicate($array){
$result = array_count_values( $array );
$values = implode(" ", array_values($result));
echo $values . "<br>";
}
$qArray = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
removeDuplicate($qArray);
One more thing, we cannot use array_unique because it includes the value which is repeated and in question we totally remove them from the current array.
Assuming the value may not appear 3+ times anywhere in the array:
$array = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
// create array indexed by the numbers to remove
$remove = array_filter(array_count_values($array), function($value) {
return $value >= 3;
});
// filter the original array
$results = array_values(array_filter($array, function($value) use ($remove) {
return !array_key_exists($value, $remove);
}));
If values may not appear 3+ times consecutively:
$results = [];
for ($i = 0, $n = count($array); $i != $n;) {
$p = $i++;
// find repeated characters
while ($i != $n && $array[$p] === $array[$i]) {
++$i;
}
if ($i - $p < 3) {
// add to results
$results += array_fill(count($results), $i - $p, $array[$p]);
}
}
This should work :
function removeDuplicate($array) {
foreach ($array as $key => $val) {
$new[$val] ++;
if ($new[$val] >= 3)
unset($array[$key]);
}
return $array;
}
run this function i hope this help..
function removeDuplicate($array){
$result = array_count_values( $array );
$dub = array();
$answer = array();
foreach($result as $key => $val) {
if($val >= 3) {
$dub[] = $key;
}
}
foreach($array as $val) {
if(!in_array($val, $dub)) {
$answer[] = $val;
}
}
return $answer;
}
You can use this function with any number of occurrences you want - by default 3
function removeDuplicate($arr, $x = 3){
$new = $rem = array();
foreach($arr as $val) {
$new[$val]++;
if($new[$val]>=$x){
$rem[$val]=$new[$val];
}
}
$new = array_keys(array_diff_key($new, $rem));
return $new;
}
I think it is getting correct output. just try once.
$array=array(1,2,3,7,4,4,3,5,5,6,7);
$count=count($array);
$k=array();
for($i=0;$i<=$count;$i++)
{
if(!in_array($array[$i],$k))
{
$k[]=$array[$i];
}
}
array_pop($k);
print_r($k);
in this first $k is an empty array,after that we are inserting values into $k.
You should use array_unique funciton.
<?php
$q = array(1, 3, 5, 2, 6, 6, 6, 3, 1, 9);
print_r(array_unique($q));
?>
Try it and let me know if it worked.
Related
I have to find the difference for both array, i have tried but i am not getting my expected.can you please help here.
<?php
$firstArray = array(3,4,5,6);
$secondArray = array(4, 5);
$outputArray = array();
foreach($firstArray as $firstArrayItem) {
foreach($secondArray as $secondArrayItem) {
if($firstArrayItem != $secondArrayItem) {
$outputArray[] = $firstArrayItem;
}
}
}
print_r($outputArray);
Expected output
[3,6]
Note: we should not use PHP inbuild functions.
a) Use a boolean value to check it:
<?php
$firstArray = array(3,4,5,6);
$secondArray = array(4, 5);
$outputArray = array();
foreach($firstArray as $firstArrayItem) {
$found = false;
foreach($secondArray as $secondArrayItem) {
if($firstArrayItem == $secondArrayItem) {
$found = true;
}
}
if($found == false){
$outputArray[] = $firstArrayItem;
}
}
print_r($outputArray);
Output: https://3v4l.org/8CuS8
b) Another way using for() loop: https://3v4l.org/9JZrW
Here is a solution that does not give a different result when the input arrays as swapped.
So it will give the same result for this:
$firstArray = array(3, 4, 5, 6);
$secondArray = array(4, 5);
as for this:
$firstArray = array(4, 5);
$secondArray = array(3, 4, 5, 6);
Also, it will deal with duplicate values as follows:
$firstArray = array(4, 4, 4);
$secondArray = array(3, 4, 6);
for which I would expect the output to be:
array(3, 4, 4, 6) // Only one 4 is common and should be excluded.
The code for having this behaviour could be (without inbuilt functions except for print_r):
$assoc = [];
foreach($firstArray as $value) {
$assoc[$value][] = 1;
}
foreach($secondArray as $value) {
$assoc[$value][] = -1;
}
foreach($assoc as $value => $list) {
$count = 0;
foreach($list as $delta) $count += $delta;
for($i = $count > 0 ? $count : -$count; $i > 0; $i--) {
$result[] = $value;
}
}
print_r($result);
$firstArray = array(3,4,5,6);
$secondArray = array(4, 5, 7);
$outputArray = diffArray($firstArray, $secondArray);
$outputArray = diffArray($secondArray, $firstArray, $outputArray);
function diffArray($arr1, $arr2, $result = array()) {
foreach($arr1 as $arr11) {
$isExist = false;
foreach($arr2 as $arr22) {
if($arr11 === $arr22) {
$isExist = true;
}
}
if(!$isExist) $result[] = $arr11;
}
return $result;
}
print_r($outputArray);
For example, in php
$arr = [9, 4, 3, 5, 2, 6];
then,
$output = [[0,0], [1,2], [2,2], [1,1], [4,1], [1,0]];
[0, 0] = the bigger elements of 9 is 0 on both side
[1, 2] = the bigger elements of 4 is 1 (9) on left and 2 (5, 6) on right side ... [ 9 > 4] - [ 5 > 4, 6 > 4 ]
[2, 2] = the bigger elements of 3 is 2 (9, 4) on the left and 2 (5, 6) on right side
[1, 1] = the bigger elements of 5 is 1 (9) on the left and 1 (6) on the right side
[4, 1] = the bigger elements of 2 (9, 4, 3, 5) is 4 on the left and 1 (6) on the right side
[1, 0] = the bigger elements of 6 is 1 (9) on the left and 0 (no elements after 6) on the right side
I want it in O(n log(n)), is it possible?
Looks like lots of answers already, you could do this with some of the array functions like some of the answers did, but since this is probably for your homework best to keep it simple. I keep track of whether it's left or right that should be incremented each iteration by using the $side variable.
$arr = [9, 4, 3, 5, 2, 6];
$results = [];
for ($x = 0; $x < count($arr); $x++) {
$results[$x] = [0,0];
$side = 0;
for ($y = 0; $y < count($arr); $y++) {
if ($arr[$y] > $arr[$x]) {
$results[$x][$side]++;
} elseif ($arr[$x] == $arr[$y]) {
$side = 1;
}
}
}
You need to loop through $arr to get each value, and then, in the loop, loop again through $arr to get the other values. Then, in the second loop, you build your output array by comparing both the value (to know if the number is, indeed, bigger) and the key (to know if it's on the left or the right).
$arr = array(9, 4, 3, 5, 2, 6);
$output = array();
foreach ($arr as $key=>$value) {
$out = array(0, 0);
foreach ($arr as $key2=>$value2) {
if ($key2 == $key) # If it's the same element
continue;
if ($value2 > $value) {
if ($key2 < $key)
$out[0]++;
else
$out[1]++;
}
}
$output[] = $out;
}
print_r($output);
See the output here.
Try this:
function fix_array($array) {
$return_array = array();
foreach ($array as $i => $value){
$left = array_slice($array, 0, $i);
$count_left = count(array_filter($left, function($var) use($value){
return $var > $value;
}));
$right = array_slice($array, $i + 1);
$count_right = count(array_filter($right, function($var) use($value){
return $var > $value;
}));
$return_array[] = [$count_left, $count_right];
}
return $return_array;
}
$arr = [9, 4, 3, 5, 2, 6];
$new_array = fix_array($arr);
print_r($new_array);
Simply compare it with left and right values. Try this:
$arr = [9, 4, 3, 5, 2, 6];
$total = count($arr);
$new_arr=array();
foreach ($arr as $key => $value) {
$left = 0;
$right = 0;
for ($i=0; $i < $total; $i++) {
if($key > $i && $arr[$i] > $arr[$key])
{
$left++;
}
elseif ($key < $i && $arr[$i] > $arr[$key]) {
$right++;
}
}
$new_arr[]=[$left,$right];
}
echo "<pre>";
print_r($new_arr);
Try the following code using array_walk()
<?php
$arr = [9, 4, 3, 5, 2, 6];
$finalArray =[];
array_walk($arr, function($value,$key) use(&$finalArray,&$arr) {
$param ['pre_val']=0;
$param ['post_val']=0;
$param ['current_index'] = $key;
$param ['current_value'] = $value;
$arr2 = $arr;
array_walk($arr2, function(&$value,$key) use(&$finalArray,&$param) {
if($key < $param['current_index']){
if($value > $param['current_value']){$param['pre_val'] ++;}
}else{
if($value > $param['current_value']){$param['post_val'] ++;}
}
$finalArray[$param['current_index']][0] = $param['pre_val'];
$finalArray[$param['current_index']][1] = $param['post_val'];
});
});
print_r($finalArray);
The array given has duplicate elements, So basically, I want to find the index of the last occurrence of the element I've searched.
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
$x = 4; // number to search
$low = 0;
$high = count($arr)-1;
// I want this function to return 6 which is the index of the last occurrence of 4, but instead I'm getting -1 .
function binary_search_last_occ($arr, $x, $low, $high)
{
while ($low <=$high)
{
$mid = floor(($low+$high)/2);
$result = -1;
if ($arr[$mid] == $x)
{
// we want to find last occurrence, so go for
// elements on the right side of the mid
$result = $mid;
$low = $mid+1;
}
else if($arr[$mid] > $x)
{
$high = $mid-1;
}
else
$low = $mid+1;
}
return $result;
}
echo binary_search_last_occ($arr, $x, $low, $high); // outputs -1 ?
I'm not sure, why I'm getting -1. Any suggestions?
I didn't seen your loop but I think this'll really simple to use to gain such functionality
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
$result = [];
foreach($arr as $key => $val){
$result[$val][] = $key;
}
echo end($result[4]);//6
Or you can simply use the asort function along with array_search like as
$arr = array(2, 3, 4, 4, 5, 6, 4, 8);
asort($arr);
echo array_search(4,$arr);//6
First of all for binary search your array must be sorted, if your array is not sorted you can use simple method like
function binary_search_last_occ($arr, $x, $low, $high)
{
$last_occ = -1;
while ($low <=$high)
{
if($arr[$low] == $x)
$last_occ = $low;
$low++;
}
return $last_occ ;
}
And also define $result above while() to avoid overriding every time with -1. Hence you get the result as -1.
$result = -1;
while ($low <=$high)
{
$mid = floor(($low+$high)/2);
if ($arr[$mid] == $x)
{
// we want to find last occurrence, so go for
// elements on the right side of the mid
$result = $mid;
$low = $mid+1;
}
else if($arr[$mid] > $x)
{
$high = $mid-1;
}
else
$low = $mid+1;
}
return $result;
I want to merge / mix two arrays together, but I want it to be randomly "mixed" however NOT shuffled. For example:
$first = [1, 2, 3, 4];
$second = [10, 20, 30, 40];
Possible desired "mixes" are:
[1, 10, 20, 30, 2, 40, 3, 4]
[10, 1, 2, 20, 30, 3, 4, 40]
[1, 2, 3, 10, 20, 4, 30, 40]
Note that if we pluck the values back out we would still have the original orders of:
1, 2, 3, 4
10, 20, 30, 40
Well, there's (yet another) one possible approach:
$arr = call_user_func_array('array_merge', array_map(null, $first, $second));
print_r($arr); // [1, 10, 2, 20, 3, 30, 4, 40];
Demo. That's apparently deterministic; for random ordering each pair should be shuffled additionally. For example:
function zipShuffle($first, $second) {
return call_user_func_array('array_merge', array_map(function($a, $b) {
return mt_rand(0, 1) ? [$a, $b] : [$b, $a];
}, $first, $second));
}
... but that obviously won't be able to churn out something like [1,2,3,10,20...]. If this is required, here's another solution:
function orderedShuffle($first, $second) {
$results = [];
$stor = [$first, $second];
$i = mt_rand(0, 1);
// switching between arrays on the fly
while (list(, $v) = each($stor[$i])) {
$results[] = $v;
$i = mt_rand(0, 1);
}
// one array is gone, now we need to add all the elements
// of the other one (as its counter left where it was)
$i = 1 - $i;
while (list(, $v) = each($stor[$i])) {
$results[] = $v;
}
return $results;
}
Demo. The last function is actually quite easy to extend for as many arrays as required.
Can you try this,
<?php
$first = array(1,2,3,4);
$second = array(10,20,30,40);
$arrayMixed=array();
$firstReverse=array_reverse($first);
$secondReverse=array_reverse($second);
$firstReverseCount = count($firstReverse);
$secondReverseCount = count($secondReverse);
foreach($firstReverse as $key=>$val) {
if ($firstReverseCount>0) {
array_push($arrayMixed, array_pop($firstReverse));
if ($secondReverseCount>0) {
array_push($arrayMixed, array_pop($secondReverse));
}
}
}
$ArrayMixeddata = array_merge($arrayMixed, $second);
echo "<pre>";
print_r($ArrayMixeddata);
echo "</pre>";
?>
Not quick ways, but them works.
// with permutations
function combineShuffleOrder($first, $second)
{
// combine into one array with alternation
$firstLen = count($first);
$secondLen = count($second);
$max = max($firstLen, $secondLen);
$result = array();
for($i=0; $i < $max; $i++) {
if ($i < $firstLen)
$result[] = $first[$i];
if ($i < $secondLen)
$result[] = $second[$i];
}
// "shuffle" with permutation
$len = count($result);
for($i=1; $i<$len; $i++) {
if (rand(1, 3) % 2 == 0) {
$tmp = $result[$i-1];
$result[$i-1] = $result[$i];
$result[$i] = $tmp;
$i++; // skip one exchange
}
}
return $result;
}
// with using "shuffle" in subarray
function combineShuffleOrder2($first, $second)
{
// combine into one array with alternation
$firstLen = count($first);
$secondLen = count($second);
$max = max($firstLen, $secondLen);
$result = array();
for($i=0; $i < $max; $i++) {
$sub = array();
if ($i < $firstLen)
$sub[] = $first[$i];
if ($i < $secondLen)
$sub[] = $second[$i];
shuffle($sub);
$result = array_merge($result, $sub);
}
return $result;
}
// with using "shuffle" in subarray if sources arrays are equals by length
function combineShuffleOrder3($first, $second)
{
$max = count($first);
$result = array();
for($i=0; $i < $max; $i++) {
$sub = array(
$first[$i],
$second[$i]
);
shuffle($sub);
$result = array_merge($result, $sub);
}
return $result;
}
$first = array(1,2,3,4);
$second = array(10,20,30,40);
print_r(combineShuffleOrder($first, $second));
print_r(combineShuffleOrder2($first, $second));
print_r(combineShuffleOrder3($first, $second));
I recommend forming a single array of the two input arrays for simpler toggling. Simply loop and consume one element from a randomly selected array and push that selection into the result array. When the pool of arrays is reduced to a single array, kill the loop and append the remaining elements of the last surviving array onto the result array.
I'll use a pool of four arrays (one which is empty from the beginning) to demonstrate that the snippet is robust enough to handle a variable number of arrays, a variable number of elements in each array, and empty arrays.
Code: (Demo)
$first = [1, 2, 3, 4];
$second = [10, 20, 30, 40];
$third = [];
$fourth = ['a', 'b'];
$pool = [$first, $second, $third, $fourth];
$result = [];
while (count($pool) > 1) {
$pullFrom = array_rand($pool);
if (!$pool[$pullFrom]) {
unset($pool[$pullFrom]);
continue;
}
$result[] = array_shift($pool[$pullFrom]);
}
var_export(array_merge($result, ...$pool));
Alternatively without array_merge() and count() calls, but it makes more iterated calls of array_shift(): (Demo)
$pool = [$first, $second, $third, $fourth];
$result = [];
while ($pool) {
$pullFrom = array_rand($pool);
if (!$pool[$pullFrom]) {
unset($pool[$pullFrom]);
continue;
}
$result[] = array_shift($pool[$pullFrom]);
}
var_export($result);
Loop this script until both arrays are done.
$j = 0;
$i = 0;
$r = rand(0, 1);
if($r == 0) {
$ret .= $array1[$i];
$i++;
} elseif($r == 1) {
$ret .= $array2[$j];
$j++;
}
Of course, you have to handle a few exceptions in this code, but it might be the route.
I have an array that contains multiple integers, I'm interested only in integers that repeat themselves a certain number of times. For example:
$items = (0, 0, 0, 1, 1, 2, 3, 3, 3)
I want to know which item(s) are repeated exactly $number (in this example $number = 3) times (in this example new array $items = (0, 3)).
If none of the array items is repeated $number times, I need to have var $none = 1.
I know for a function array_count_values but don't know how to implement it to my case...
$number = 3;
$items = array_keys(array_filter(array_count_values($items), create_function('$n', "return \$n == $number;")));
if (!$items) {
$none = 1;
}
use array_count_values to get a pairing of how often each number occurs
filter this through an array_filter callback that discards all entries except those that have a count of $number
take the keys of the resulting array (the actual counted values)
the resulting array is either empty or contains the values that occur $number of times
I know there are a lot of solutions, but thought I'd add one more. ;-)
function array_repeats($items,$repeats,&$none){
$result = array();
foreach (array_unique($items) as $item){
$matches = array_filter($items,create_function('$a','return ($a=='.$item.');'));
if (count($matches) == $repeats)
$result[] = $item;
}
$none = (count($result)?1:0);
return $result;
}
DEMO
$repeated_items will be an array containing only your desired items.
$limit = 3; //your limit for repetition
$catch = array();
foreach ($items as $item){
if(array_key_exists($item, $catch)){
$catch[$item]++;
} else {
$catch[$item] = 1;
}
}
$repeated_items = array();
foreach ($catch as $k=>$caught){
if($caught>=$limit){
$repeated_items[]=$k;
}
}
Some pseudo-code to get you started:
Sort your array in order to get similar items together
Foreach item
if current item == previous item then
repeat count ++
else
if repeat count > limit then
add current item to new array
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$count = array_count_values($items);
$number = 3;
$none = 1;
$result = array();
foreach(array_unique($items) as $item) {
if($count[$item] == $number) {
$result[] = $item;
$none = 0;
}
}
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$none=1;
$new_array=array();
$n=3;
dojob($items,$n,$none,$new_array);
function dojob($items,$n,&$none,&$new_array)
{
$values_count=array_count_values($items);
foreach($values_count as $value => $count)
{
if($count ==$n)
{
$none=0;
$new_array[]=$value;
}
}
}
A bit late, but:
<?php
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$temp = array_unique($items);
$result = array();
$none = 1;
$number = 3;
foreach($temp as $tmp)
{
if(count(array_keys($items, $tmp)) == $number)
{
array_push($result,$tmp);
$none = 0;
}
}
print_r($result);
?>
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$icnt = array_count_values($items);
function eq3($v) {
return $v==3;
}
var_export(array_filter($icnt, 'eq3'));
will produce array ( 0 => 3, 3 => 3, ). In your example 0 and 3 repeat 3 times. Array_filter is needed here to, actually, filter your resulting array and get rid of necessary values, but you was right about using array_count_values here.
One way would be to create a kind of hash table and loop over every item in your array.
$items = array(0, 0, 0, 1, 1, 2, 3, 3, 3);
$number = 3;
$none = 1;
foreach ($items as $value) {
if ($hash[$value] >= $number) {
# This $value has occured as least $number times. Lets save it.
$filtered_items[] = $value;
# We have at least one item in the $items array >= $number times
# so set $none to 0
$none = 0;
# No need to keep adding
continue;
} else {
# Increment the count of each value
$hash[$value]++;
}
}
$items = $filtered_items;