I am a beginner and my teacher assigned us a problem where we are given an array named arr. We are supposed to create a new array that only has the odd values from arr. This is my code:
However, say for example that arr had only even integers to begin with. My new array will be filled with 0's.Code Java:public int [] youMakeMeOdd(int [] arr) { int[] ans = new int[arr.length]; for (int i = 0; i<arr.length; i++) { if(arr[i] % 2 != 0) { ans[i] = arr[i]; } } return ans; }
But it is supposed to not have the zeros.
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.
