This is an interview question:
What is the best way to return the first repeated element out of the array of integers?
Example:
Given an array [12, 46, 244, 0, 12, 83, 48, 98, 233, 83, 26, 91, 119, 148, 98]
.
The return value in this case is 12
.
How can this be done?
Or you could use array_count_values() function to fetch that.
See here on PHP Manual.
This will give you all the duplicate values and their original positions:
result:
a loopless solution using recursion . I think it will be fastest but take more memory. Fast because size of array keep on decreasing as we move ahead hence less workload on cpu.
i think that if you look of performance, foreach loop is the faster