Remove all the prime numbers from the given array | Javascript implementation of the approach ; Function for Sieve of Eratosthenes ; Function to print the elements of the array ; Function to remove all the prime numbers ; Generate primes ; Traverse the array ; If the current element is prime ; Shift all the elements on the right of it to the left ; Decrease the loop counter by 1 to check the shifted element ; Decrease the length ; Print the updated array ; Driver code
const sz = 1e5 ; let isPrime = new Array ( sz + 1 ) ; function sieve ( ) { isPrime . fill ( true ) isPrime [ 0 ] = isPrime [ 1 ] = false ; for ( let i = 2 ; i * i <= sz ; i ++ ) { if ( isPrime [ i ] ) { for ( let j = i * i ; j < sz ; j += i ) { isPrime [ j ] = false ; } } } } function printArray ( arr , len ) { for ( let i = 0 ; i < len ; i ++ ) { document . write ( arr [ i ] + ' ' ) ; } } function removePrimes ( arr , len ) { sieve ( ) ; for ( let i = 0 ; i < len ; i ++ ) { if ( isPrime [ arr [ i ] ] ) { for ( let j = i ; j < len ; j ++ ) { arr [ j ] = arr [ j + 1 ] ; } i -- ; len -- ; } } printArray ( arr , len ) ; } let arr = [ 4 , 6 , 5 , 3 , 8 , 7 , 10 , 11 , 14 , 15 ] ; let len = arr . length ; removePrimes ( arr , len ) ;