Remove duplicate element in a Java array.

Java Programming Java8Object Oriented Programming

Following is the required program.

Example

Live Demo

import java.util.Arrays;
public class Tester {
   public static int[] removeDuplicateElements(int arr[]){
      int n = arr.length;
      int[] temp = new int[n];
      int j = 0;
      for (int i=0; i<n-1; i++){
         if (arr[i] != arr[i+1]){
            temp[j++] = arr[i];
         }
      }
      temp[j++] = arr[n-1];    
     
      return temp;
       
    }
     
    public static void main (String[] args) {
       int arr[] = {10,70,30,90,20,20,30,40,70,50};
       //sort the array
       Arrays.sort(arr);
       int[] result = removeDuplicateElements(arr);
       //printing array elements
       for (int i=0; i<result.length; i++)  {
          if(result[i] != 0){
             System.out.print(result[i]+" ");
          }        
       }
   }
}

Output

10 20 30 40 50 70 90
raja
Published on 22-Apr-2018 21:25:09
Advertisements