BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to remove duplicate elements in an Array

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to write java program to remove duplicate elements in a given array. We will write two java programs in this article. First program will remove the duplicate elements from a sorted array and the second program will remove the duplicates from unsorted array.

Illustration:

Array: {1, 2, 2, 3, 3, 4}
Array after removing duplicates: {1, 2, 3, 4}

Example 1: Program to remove duplicate elements from a sorted array

In this example, the given array is a sorted array. To remove the duplicates from an unsorted array, refer the second example.

public class JavaExample{
  public static int removeDuplicates(int arr[], int count){
    if (count==0 || count==1){
      return count;
    }
    // creating a temporary array to hold non-duplicate elements
    int[] temp = new int[count];
    int j = 0;
    for (int i=0; i<count-1; i++){
      if (arr[i] != arr[i+1]){
        temp[j++] = arr[i];
      }
    }
    temp[j++] = arr[count-1];
    // copying the temp array to the original array
    for (int i=0; i<j; i++){
      arr[i] = temp[i];
    }
    return j;
  }

  public static void main (String[] args) {
    int arr[] = {1, 2, 2, 3, 4, 5, 5, 5};
    System.out.print("Original Array: ");
    int length = arr.length;
    for (int i=0; i<length; i++)
      System.out.print(arr[i]+" ");

    //getting the new array size after removing duplicates
    length = removeDuplicates(arr, length);

    //for new line
    System.out.println("");

    //Displaying array with non-duplicate elements
    System.out.print("Array after removing duplicate elements: ");
    for (int i=0; i<length; i++)
      System.out.print(arr[i]+" ");
  }
}

Output:

Original Array: 1 2 2 3 4 5 5 5 
Array after removing duplicate elements: 1 2 3 4 5 

Example 2: Program to remove duplicate elements from an unsorted array

This program is same as the above program except that here the given array is unsorted. The workaround is pretty simple, we can import java.util.Arrays package and use the Arrays.sort() method to sort the unsorted array and then use the same logic that we have used above to remove duplicate elements.

import java.util.Arrays;
public class JavaExample{
  public static int removeDuplicates(int arr[], int count){
    if (count==0 || count==1){
      return count;
    }
    // creating a temporary array to hold non-duplicate elements
    int[] temp = new int[count];
    int j = 0;
    for (int i=0; i<count-1; i++){
      if (arr[i] != arr[i+1]){
        temp[j++] = arr[i];
      }
    }
    temp[j++] = arr[count-1];
    // copying the temp array to the original array
    for (int i=0; i<j; i++){
      arr[i] = temp[i];
    }
    return j;
  }

  public static void main (String[] args) {
    int arr[] = {3, 2, 1, 2, 9, 10, 4, 10, 9};
    System.out.print("Original Array: ");

    //Sorting the given unsorted array
    Arrays.sort(arr);
    int length = arr.length;
    for (int i=0; i<length; i++)
      System.out.print(arr[i]+" ");

    //getting the new array size after removing duplicates
    length = removeDuplicates(arr, length);

    //for new line
    System.out.println("");

    //Displaying array with non-duplicate elements
    System.out.print("Array after removing duplicate elements: ");
    for (int i=0; i<length; i++)
      System.out.print(arr[i]+" ");
  }
}

Output:

Original Array: 1 2 2 3 4 9 9 10 10 
Array after removing duplicate elements: 1 2 3 4 9 10 

Recommended Java Programs

  1. java program to print number of elements in an array
  2. java program to find smallest number in an array
  3. java program to find largest element of an array
  4. java program to print the duplicate elements of an array
❮ Java TutorialJava Programs ❯

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Programs

  • C Programs
  • Java Programs
  • C++ Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap