Java Initialize index i=0, iterate the array and if the element is not equals to val, add it to i index, increment the index i. Return i.