I need to do an algorithm that search a specific int in array of int.
That number must appear >= than arraySize/2 times.
example: [] = 4 4 3 5 5 5 5 5 5 6
arraysize: 10
number 5 exists 6x -> so this is the result of algorithm
but I need to do this without additionam memory, and in time O(n) -> in one pass.
Is this even possible? Any suggestions how to start it?
It is indeed possible; the task is known as “Dominant Element,” and used for interviews and as a homework. Read the article below for a proper analysis; the solution itself is simple but not easy: proving that it indeed does what it promises is not quite trivial (unless of course you know the answer).
http://www.cse.iitk.ac.in/users/sbaswana/Courses/ESO211/problem.pdf
Note though that the time is O(n), but as far as I’m aware, it is not possible to do it in one pass unless you know for sure there is a dominant element.
As of additional memory, you will need memory for
i, the counter;x, the element to check and return; andcount, the size of the imaginary working set. That’s O(1) and is usually considered OK for such problems.