Elements that occurred only once in the array | Function to find the elements that appeared only once in the array ; Check if the first and last element is equal . If yes , remove those elements ; Start traversing the remaining elements ; Check if current element is equal to the element at immediate previous index If yes , check the same for next element ; Else print the current element ; Check for the last element ; Driver code
< ? php function occurredOnce ( & $ arr , $ n ) { $ i = 1 ; $ len = $ n ; if ( $ arr [ 0 ] == $ arr [ $ len - 1 ] ) { $ i = 2 ; $ len -- ; } for ( ; $ i < $ n ; $ i ++ ) if ( $ arr [ $ i ] == $ arr [ $ i - 1 ] ) $ i ++ ; else echo $ arr [ $ i - 1 ] . " ▁ " ; if ( $ arr [ $ n - 1 ] != $ arr [ 0 ] && $ arr [ $ n - 1 ] != $ arr [ $ n - 2 ] ) echo $ arr [ $ n - 1 ] ; } $ arr = array ( 7 , 7 , 8 , 8 , 9 , 1 , 1 , 4 , 2 , 2 ) ; $ n = sizeof ( $ arr ) ; occurredOnce ( $ arr , $ n ) ; ? >