+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

11/9/2019 4:36:17 PM

Sarah

3 Answers

New Answer

+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.

+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).

+1

Thank you very much... Your code's correctness is just perfect. I should learn more about array functions.