Skip to main content

Removing Duplicates from Sorted Array Example in Java

2 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Simple Java program for Removing duplicates from sorted array is a classic Java console program that demonstrates the concept with complete source code and sample output. Arrays store fixed-size sequences with fast index access — a foundation before collections.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Definition

A data structure consisting of a collection of elements (values or variables), each identified by an index or key is called an Array. The position of each element in an array can be computed from its index.

Syntax

Data_type[] Variable_name = new Data_type[Length];

Removing duplicates from sorted array Program

public class RemoveDuplicatesFromSortedArray {
    public static int[] removeDuplicates(int[] input){
        int j = 0;
        int i = 1;
        
        if(input.length<2){
            return input;
        }
        while (i<input.length) {            
            if(input[i] == input[j]){
                i++;
            }else{
                input[++j] = input[i++];
            }
        }
        int[] output = new int[j+1];
        for( int k = 0 ; k < output.length; k++){
            output[k] = input[k];
        }
        return output;
    }
    
    public static void main(String[] args) {
        int[] input = {2,3,66,6,8,9,10,10};
        int[] output = removeDuplicates(input);
        for(int i : output){
            System.out.println(i+" ");
        }
    }
}

Sample Output

2 
3 
66 
6 
8 
9 
10

When to use

Use this simple java program for removing duplicates from sorted array example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. int j = 0; updates a variable used in the calculation or output.

  3. int i = 1; updates a variable used in the calculation or output.

  4. The if statement runs the nested code only when the condition is true.

  5. The if statement runs the nested code only when the condition is true.

  6. input[++j] = input[i++]; updates a variable used in the calculation or output.

  7. int[] output = new int[j+1]; updates a variable used in the calculation or output.

  8. A loop repeats the block until its condition becomes false.

Best Practices

  • Use meaningful variable and class names that describe their purpose.
  • Compile and run the program locally — modify values to see how output changes.
  • Read compiler errors carefully; they usually point to the exact line to fix.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Simple Java program for Removing duplicates from sorted array program demonstrate?
It shows how to implement simple java program for removing duplicates from sorted array in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials