top of page
Search

# Move Zeroes

Updated: Mar 24, 2021

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

`Input: [0,1,0,3,12]Output: [1,3,12,0,0]`

Note:

1. You must do this in-place without making a copy of the array.

2. Minimize the total number of operations.

Solution:

```class Solution {
public void moveZeroes(int[] nums) {
int j = -1;
for (int i = 0; i < nums.length; i++)
{
if (nums[i] != 0)
{
j++;
int temp = nums[i];
nums[i]=nums[j];
nums[j]=temp;

}
}
}
}```
{"ft_lang_label":"__label__en","ft_lang_prob":0.5537844,"math_prob":0.99418604,"size":525,"snap":"2023-40-2023-50","text_gpt3_token_len":166,"char_repetition_ratio":0.09980806,"word_repetition_ratio":0.0,"special_character_ratio":0.35619047,"punctuation_ratio":0.21875,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9901226,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-09-22T16:01:57Z\",\"WARC-Record-ID\":\"<urn:uuid:a31a1c83-9781-4953-addd-1c329c419d4d>\",\"Content-Length\":\"944571\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:12db876a-6e4c-4783-94a3-411d6ac4a22d>\",\"WARC-Concurrent-To\":\"<urn:uuid:268a84a9-3709-4592-8df2-97aa93651747>\",\"WARC-IP-Address\":\"34.149.87.45\",\"WARC-Target-URI\":\"https://www.coderscamp.tech/post/move-zeroes\",\"WARC-Payload-Digest\":\"sha1:C4EY6F737MIDOIZHK7DCZAK4GRDB3T2D\",\"WARC-Block-Digest\":\"sha1:LQK53MZE3ZHIO3GCOCGJQ6CXBAJU24II\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-40/CC-MAIN-2023-40_segments_1695233506420.84_warc_CC-MAIN-20230922134342-20230922164342-00628.warc.gz\"}"}