QaMantra: How to remove Duplicate value from Array in Java

How to remove Duplicate value from Array in Java

package com.abof.scripts;

public class JavaProgram {
 /**
  * 
  * @author   QaMantra
  */
 
 
 // program written for remove duplicate value
 public static void main(String[] args) {
  
  {
       
            int array[] = { 10, 20, 30, 20, 40, 40, 50, 30, 10, 50 };// array of ten    
            // elements
            int size = array.length;
            System.out.println("Size before deletion: " + size);
 
            for (int i = 0; i < size; i++) 
            {
 
                for (int j = i + 1; j < size; j++) 
                {
 
 // To Check i indeex value is equal to j index value or not 
                    if (array[i] == array[j]) 
                    {
                        while (j < (size) - 1) 
                        {
                         // swaping the value
                            array[j] = array[j + 1];
                            j++;
                        }   
                        size--;
                    }
                }
            }
            System.out.println("Size After deletion: " + size);
 
            for (int m = 0; m < size; m++) 
            {
                System.out.println(array[m]); // printing the values
            }
    }
  
  
 }

}
output :                                                        Size before deletion: 10
Size After deletion: 5
10
20
30
40
50

                                       Top Java Program Asked in Selenium Interview Questions and Answers  


No comments: