Find the element in array that appears once using php [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I've got many answer about this question using other language but i want an answer in php language. Any one help me please
This is my array look like
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];

Use array_count_values() like below:-
<?php
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
$array_count_values = array_count_values($array);// get how many times a value appreas inside array
foreach($array_count_values as $key=>$val){ // now iterate over this newly created array
if($val ==1){ // if count is 1
echo $key. " in array come only one time.\n"; // this means value appears only one time inside array
}
}
Output:- https://eval.in/867433 OR https://eval.in/867434
If you want values in an array:-
<?php
$array = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11,13]; // increased one value to show you the output
$array_count_values = array_count_values($array);
$single_time_comming_values_array = [];
foreach($array_count_values as $key=>$val){
if($val ==1){
$single_time_comming_values_array[] = $key;
}
}
print_r($single_time_comming_values_array);
Output:- https://eval.in/867515

Here, you can use something like this-
<?php
function appearedOnce($arr)
{
$result = 0;
for($i=0; $i<sizeof($arr); $i++)
{
$result = $result ^ $arr[$i];
}
return $result;
}
$num = array(1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11);
print_r(appearedOnce($num)."\n")
?>

My initial response was to take a more pedestrian approach which works as you may note from this example. Then I chanced upon a related discussion.
Another approach involves sorting the array and then inspecting pairs of numbers for duplicates. The following code is a result of coupling the OP's array with my translation of the presumably C source code of Michael Martin into PHP, as follows:
<?php
$arr = [1, 2, 3, 4, 4, 1, 2, 5, 5, 11, 11];
sort($arr);
for($i = 0, $max = count($arr); $i < $max; $i++){
// is single number last element in array?
if($i == count($arr)-1)
$singleNum = $arr[$i];
// If adjacent elements the same, skip
if($i < count($arr)-1 && $arr[$i] == $arr[$i+1]){
$i++;
}
else
{
// found single number.
$singleNum = $arr[$i];
}
}
var_dump($singleNum);
See live code

Related

Remix the string based on array

I have a simple whiteboard question:
Create a function that takes both a string and an array of numbers as arguments. Rearrange the letters in the string to be in the order
specified by the index numbers. Return the "remixed" string. Examples
remix("abcd", [0, 3, 1, 2]) ➞ "adbc"
I submitted my code but it's not being accepted and I am unable to see why. I wrote:
function remix($str, $arr) {
$strArr = str_split($str);
for($i = 0; $i < count($strArr); $i++) {
$arr[$i] = $strArr[$arr[$i]];
}
return implode("", $arr);
}
remix("computer", [0, 2, 1, 5, 3, 6, 7, 4]); // ➞ "cmourpte"
Can anyone see something wrong with it? It's always regular characters, no special cases FYI. Very confused.
It looks like the assignment part is putting the value the wrong way round (i.e. it should be assigning the char at position $i to $arr[$i])...
function remix($str, $arr) {
$strArr = str_split($str);
for($i = 0; $i < count($strArr); $i++) {
$strArr[$arr[$i]] = $str[$i];
}
return implode("", $strArr);
}
which for
echo remix("PlOt", [1, 3, 0, 2]).PHP_EOL;
echo remix("computer", [0, 2, 1, 5, 3, 6, 7, 4]);
gives
OPtl
cmourpte
Just to check, this passes the test.
Well it would seem you missed that the remix was giving an incorrect result.
But I would have done it this way as by default a PHP String is in fact an array already.
function remix($str, $arr) {
$mixed = '';
foreach ($arr as $i) {
$mixed .= $str[$i];
}
return $mixed;
}
echo remix("computer", [0, 2, 1, 5, 3, 6, 7, 4]);
RESULT
cmotperu
In fact if this is the question, the stated answers are wrong in ALL cases.
Create a function that takes both a string and an array of numbers as arguments.
Rearrange the letters in the string to be in the order specified by the index numbers.
Return the "remixed" string.
Examples
remix("abcd", [0, 3, 1, 2]) ➞ "acdb"
The string you'll be returning will have: "a" at index 0, "b" at index 3, "c" at index 1, "d" at index 2, because the order of those characters maps to their corresponding numbers in the index array.
remix("PlOt", [1, 3, 0, 2]) ➞ "OPtl"
remix("computer", [0, 2, 1, 5, 3, 6, 7, 4]) ➞ "cmourpte"

Custom sorting in PHP Array - Like mysql ORDER BY FIELD(order_fied, 4, 1, 5, 2, 3) [duplicate]

This question already has answers here:
How can I sort arrays and data in PHP?
(14 answers)
Closed 7 years ago.
I have one dynamic array, for example with following elements:
$myArray = array(1, 2, 3, 4, 5);
And I want my final array to be:
$finalArray = array(4, 1, 5, [whaterever]);
What's the best way to do custom sorting which is neither ascending or descending or doesn't follow any rule like this?
Thanks for your suggestions.
[Edit]
I have edited my question.
You can try the usort function. On the second argument, you should write a function that will decide how your array elements should be placed.
Something like this:
<?php
$myArray = [1, 2, 3, 4, 5];
$properOrder = [4, 1, 5, 2, 3];
usort($myArray, function($a, $b) use($properOrder) {
$index1 = array_search($a, $properOrder);
$index2 = array_search($b, $properOrder);
if ($index1 > $index2) {
return 1;
} else if ($index1 < $index2) {
return -1;
} else {
return 0;
}
});
print_r($myArray);

PHP array_intersect() till the first match

I have 2 arrays to compare and find if there is at least a single value in common.
This works just fine:
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)) {
// good, at least one match found
}
However, the question is performance. It doesn't make sense to continue looping thru the arrays after the first match was found. Is there a native PHP function or a useful snippet to achieve this?
Will a combination of foreach() and in_array() do the trick?
How about this?
foreach ($arr1 as $key => $val) {
if (in_array($val, $arr2)){
// do something, maybe return so you wouldn't need break
break;
}
}
Just compare the first value?
$arr1 = array(1, 2, 3, 4, 5);
$arr2 = array(2, 3, 4, 5, 6);
if (array_intersect($arr1, $arr2)[0]) {
// good, at least one match found
}

Merge two arrays alternatively [duplicate]

This question already has answers here:
Merge two flat indexed arrays of equal size so that values are pushed into the result in an alternating fashion
(2 answers)
Closed 6 years ago.
What I want is an efficient (without looping) way to merge arrays in the way that the first element of the resulting array is the first element of the first array, the second element of the resulting array is the second element of the second array (alternatively)... etc
Example:
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$resultingArray = array(1, 2, 3, 4, 5, 6);
assuming both arrays have the same length.
$arr1 = array(1, 3, 5);
$arr2 = array(2, 4, 6);
$new = array();
for ($i=0; $i<count($arr1); $i++) {
$new[] = $arr1[$i];
$new[] = $arr2[$i];
}
var_dump($new);
Not that I'd really advocate this "hack", but this'll do:
$result = array();
array_map(function ($a, $b) use (&$result) { array_push($result, $a, $b); }, $arr1, $arr2);
It really just hides a double loop behind array_map, so, meh...

dynamically build and populate table with php array

let's say I have these two arrays:
$array1 = array(1, 2, 3, 4, 5);
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
As you can see, my arrays have different lengths. What I'm trying to do is to input these array values into an HTML table with the first column containing the values coming from $array1 and the second column containing the values coming from $array2. So, in this case right here, I should have a table of 10 rows (because $array2 contains 10 elements) and 2 columns (because I have 2 arrays). Also, I cannot know in advance which array is going to have more elements than the other (so, $array1 could be bigger than $array2, they could also have equal sizes). So, depending on which array has more elements, the number of rows in my table should adjust accordingly.
Any idea please?
Thank you
$array2 = array(6, 7, 8, 9, 10, 11, 12, 13, 14, 15);
$array1 = array(1, 2, 3, 4, 5);
$a=count($array1);
$b=count($array2);
echo "<table border=1><tr><th>Array1</th><th>Array2</th></tr>";
if($a > $b)
{
for($i=0;$i<$a;$i++)
{
echo "<tr><td>".$array1[$i]."</td>";
echo "<td>".$array2[$i]."</td></tr>";
}
}
if($b > $a)
{
for($i=0;$i<$b;$i++)
{
echo "<tr><td>".$array1[$i]."</td>";
echo "<td>".$array2[$i]."</td></tr>";
}
}
echo "</table>";
Try thinking of some thing like below
this will give you atleast an idea of how to iterate them.
$array = array($array1,$arry2);
for($i = 0; $i < $array.length; $i++)
{
$rows = $array[$i];
for($j=0; $j< $rows.length; $rows++){
}
}
i hope you can figure out the logic required from this . in case it doesnt work out post comment and we will walk through

Categories