forked from mirandaio/codingbat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwithoutTen.java
More file actions
25 lines (21 loc) · 809 Bytes
/
withoutTen.java
File metadata and controls
25 lines (21 loc) · 809 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/* Return a version of the given array where all the 10's have been removed.
* The remaining elements should shift left towards the start of the array as
* needed, and the empty spaces a the end of the array should be 0. So
* {1, 10, 10, 2} yields {1, 2, 0, 0}. You may modify and return the given
* array or make a new array.
*/
public int[] withoutTen(int[] nums) {
int i = 0;
while(i < nums.length && nums[i] != 10)
i++;
for(int j = i + 1; j < nums.length; j++) {
if(nums[j] != 10) {
nums[i] = nums[j];
nums[j] = 10;
i++;
}
}
for( ; i < nums.length; i++)
nums[i] = 0;
return nums;
}