Help me show the unpaired one | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Help me show the unpaired one

There is an array which has even elements. Some of them paired, and the other is not paired. I wanna show the unpaired one. Example1 : [9,3,9,3,9,7,9] Expected : 7 Example2 : [1,3,1,2,2] Expected : 3 I think the paired ones should be removed from the old array and move to new array... Please help me... Thank you https://code.sololearn.com/wX9TgMDkB1DD/?ref=app

9th Nov 2019, 4:36 PM
Sarah
Sarah - avatar
3 Answers
+ 2
Maybe something like this: https://code.sololearn.com/w2Kkgp6gZ3Gr/#php I decided to use array_splice() function to remove an element in array because it reindexes the whole array. I don't know if I understand that correctly. Array [9, 9, 9] should output 9? Also, I decided to use is_numeric() function in the if statement because array_search() function may return 0 which is equal to false. Instead of this you can use if ($pair !== false) which additionally checks if the return value of array_search is boolean.
9th Nov 2019, 6:49 PM
Michał Trudziński
Michał Trudziński - avatar
+ 1
First of all use $i < $len instead of $i <= $len in your for loop. The last index of an default array is (array elements - 1).
9th Nov 2019, 6:17 PM
Michał Trudziński
Michał Trudziński - avatar
+ 1
Thank you very much... Your code's correctness is just perfect. I should learn more about array functions.
10th Nov 2019, 12:07 PM
Sarah
Sarah - avatar