Horje
Segregate 0s and 1s using JavaScript

Given an array, our task is to separate 0s and 1s in an array using JavaScript. We have to rearrange the array such that all 0s appear before all 1s.

Example:

Input:  array = [ 0 , 1 , 1, 0 , 1, 0, 1] 

Output: array = [0, 0, 0, 1, 1, 1, 1]

Explanation:
All zeroes are together and all ones are together in sorted manner.

Using for loop

Initialize a variable countZeroes to 0 to keep track of the number of zeroes in the array. Now Iterate through the array and increment countZeroes each time a zero is encountered. Iterate through the array again. For the first countZeroes elements, set them to 0, and for the rest of the elements, set them to 1. Return the modified array.

Example: The example below shows how to Separate 0s and 1s using Simple Counting.

JavaScript
function segregateCounting(arr) {
    let countZeroes = 0;

    // Count the number of zeroes in the array
    for (let i = 0; i < arr.length; i++) {
        if (arr[i] === 0) {
            countZeroes++;
        }
    }

    // Set the first countZeroes elements 
    // to 0 and the rest to 1
    for (let i = 0; i < countZeroes; i++) {
        arr[i] = 0;
    }
    for (let i = countZeroes; i < arr.length; i++) {
        arr[i] = 1;
    }

    return arr;
}

const arr = [0, 1, 1, 0, 1, 0, 1];
console.log(segregateCounting(arr)); 

Output
[
  0, 0, 0, 1,
  1, 1, 1
]

Time Complexity: O(n)

Space Complexity: O(1)

Using Two-pointer Approach

Use two pointers, left and right, initialized at the start and end of the array, respectively. Move left towards the right until finding a 1, and move right towards the left until finding a 0. Then, Swap these elements. Continue until left is less than right. Finally, return the modified array.

Example: The example below shows how to Separate 0s and 1s using Two-pointer Approach.

JavaScript
function segregateTwoPointer(arr) {
    let left = 0, right = arr.length - 1;
    while (left < right) {
        // Increment left pointer 
        // while corresponding element is 0
        while (arr[left] === 0 &&
               left < right) left++;
        // Decrement right pointer
        // while corresponding element is 1
        while (arr[right] === 1 &&
               left < right) right--;
        // Swap elements if left
        // is still less than right
        if (left < right) {
            [arr[left], arr[right]] = [arr[right], arr[left]];
            left++;
            right--;
        }
    }
    return arr;
}
const arr = [0, 1, 1, 0, 1, 0, 1];
console.log(segregateTwoPointer(arr)); 

Output
[
  0, 0, 0, 1,
  1, 1, 1
]

Time Complexity: O(n)

Space Complexity: O(1)




Reffered: https://www.geeksforgeeks.org


JavaScript

Related
Javascript WeakRef.prototype.deref() Javascript WeakRef.prototype.deref()
JavaScript Program to Check if all Leaf Nodes are at Same Level or Not JavaScript Program to Check if all Leaf Nodes are at Same Level or Not
JavaScript Program to Find the Tangent of given Radian Value JavaScript Program to Find the Tangent of given Radian Value
Sorting Objects by Numeric Values using JavaScript Sorting Objects by Numeric Values using JavaScript
Alternative Sorting of an Array using JavaScript Alternative Sorting of an Array using JavaScript

Type:
Geek
Category:
Coding
Sub Category:
Tutorial
Uploaded by:
Admin
Views:
17