Write efficient code for extracting unique elements from a sorted list of array. e.g.

Write, efficient code for extracting unique elements from a sorted list of array. e.g.
(1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

 

 

 


public class Unique {

 public static void main(String args[])
 {
  int[] a=new int[]{1,1,1,3,3,3,4,4,4};
  int[] b=new int[10];
  
  int j=0;
  for(int i=0;i<a.length;i++)
  {
   if(!contains(b,a[i]))
   {
    b[j]=a[i];
    j++;
   }
   
   
  }
  
  for(int i=0;i<j;i++)
  {
   System.out.print(b[i]+" ");
  }
 }
 
 
 public static boolean contains(int a[],int data)
 {
       for(int i=0;i<a.length;i++)  
       {
     if(a[i]==data)  
        return true;
       }
       return false;
 }
 
}

你可能感兴趣的:(Write efficient code for extracting unique elements from a sorted list of array. e.g.)