First i would like to tell problem with your code ,see all the elements of the array are initalized with value o

when you define it.

Now you are adding lement whenever any odd no found so it could be in any index position within your array so

this line is storing odd no at original index as in the input array.

public int [] youMakeMeOdd(int [] arr)
{
int[] ans = new int[arr.length];
int k=0;
for (int i = 0; i<arr.length; i++)
{
if(arr[i] % 2 != 0)
{
ans[k] = arr[i];
k++;
}
}
return ans;
}

Say arr was {2,4,5,6}. My new array should be {5}. Instead it is{0,0,5,0}. I need a way to get rid of the zeros. Any help would be appreciated.

In the modified code above

Output for this input will be {5,0,0,0}

now you can display elements upto first zero found.

for(int i=0;i<ans.length;i++){

System.out.println(ans[i]);

if(ans[i] == 0)

break;

}