Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,845 questions

51,766 answers

573 users

How to move all zeros in int array to the end with Python

1 Answer

0 votes
def move_zeros_to_end(arr): 
    j = 0
    ln = len(arr)
    
    for i in range(ln): 
        if arr[i] != 0: 
            arr[j] = arr[i] 
            j+=1
      
    while j < ln: 
        arr[j] = 0
        j += 1
        

        
arr = [0, 3, 4, 0, 6, 0, 0, 8, 9, 0] 
move_zeros_to_end(arr) 
print(arr) 


'''
run:

[3, 4, 6, 8, 9, 0, 0, 0, 0, 0]

'''

 



answered Feb 26, 2019 by avibootz

Related questions

1 answer 139 views
1 answer 168 views
1 answer 143 views
1 answer 146 views
2 answers 151 views
1 answer 124 views
2 answers 150 views
...