Segregate 0’s and 1’s in array

Segregate 0’s and 1’s in Array in Java

In this article, we will learn about how to segregate 0’s and 1’s in array in Java with detailed procedure and code. In the given array, we will try to separate 0’s and 1’s from each other using two different methods.

Segregate 0's and 1's

Segregate 0’s and 1’s in an array in Java

Segregating 0’s and 1’s in an array involves rearranging the elements so that all the 0’s come before all the 1’s. For instance, given an array [1, 0, 0, 1, 1, 0], segregating 0’s and 1’s would yield [0, 0, 0, 1, 1, 1]. This type of segregation is commonly applied in computer science and programming for handling binary data or flags, and it can also be used as a preprocessing step in machine learning algorithms for binary classification problems.

Segregate 0’s and 1’s in an array in Java

Algorithm

  1. Start with two empty arrays, one for storing 0’s and another for storing 1’s.
  2. Iterate through each element in the original array and do the following:
    1. If the element is 0, add it to the array for 0’s.
    2. the element is 1, add it to the array for 1’s.
  3. Combine the arrays for 0’s and 1’s to create the segregated array.

Code for Segregate 0’s and 1’s in an array in Java

Run
import java.util.Arrays;

public class Main {
    public static void segregate_01(int arr[], int n) {
        int count_0 = 0, count_1 = 0;
        for (int i = 0; i < n; i++) {
            if (arr[i] == 0) {
                count_0++;
            } else {
                count_1++;
            }
        }

        for (int i = 0; i < count_0; i++) {
            System.out.print("0 ");
        }

        for (int i = count_0; i < n; i++) {
            System.out.print("1 ");
        }
    }

    public static void main(String[] args) {
        int arr[] = {0, 1, 0, 0, 1, 1, 1, 0, 1, 1};
        int n = arr.length;
        System.out.print("Array: ");
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.print("\nSegregated array: ");
        segregate_01(arr, n);
    }
}

Output

Array: 0 1 0 0 1 1 1 0 1 1 
Segregated array: 0 0 0 0 1 1 1 1 1 1 

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription

Arrays

  • Introduction to Arrays – CC++ | Java
  • Introduction to 2-D Arrays – CC++ Java
  • Sorting of Array – C | C++ | Java
  • Array Rotation – CC++ | Java
  • Reverse an array or string- CC++ | Java
  • Find pairs in array with given sum – CC++ | Java
  • Sort the array in Waveform – CC++ | Java
  • Majority Element in Array – CC++ | Java
  • Boyer-Moore’s Voting Algorithm – CC++ | Java
  • K-pairs with smallest sum in 2 arrays – C | C++ | Java
  • Largest Sum Contigous SubArray – CC++ | Java
  • Maximum Average Sub-array of K length – CC++ | Java
  • Size of sub-array with max sum – CC++ | Java
  • Sub-array with given sum – CC++ | Java
  • Triplet that sum to a given value – CC++ | Java
  • Segregate 0’s and 1’s in array – CC++ | Java
  • Segregate 0’s 1’s and 2’s in array – CC++ | Java
  • Sort elements in array by frequency – CC++ | Java
  • Finding pythagorean triplets in an array – CC++ | Java
  • Reorder array using given indexes – CC++ | Java
  • Merging two sorted arrays – CC++ | Java
  • Minimum number of Merge Operations to make an Array Palindrome – CC++ | Java
  • Find Zeros to be Flipped so that number of Consecutive 1’s is maximized – CC++ | Java