A Java Program To Find Duplicate Values In an Array

By Saruque Ahamed Mollick

In this post, we are going to write a program to find the duplicate values in an array.
Suppose an array is holding these values {1,4,5,3,1,8,7,5,7}

This program will print all the duplicates value of this array, those values are 1,5,7

Java program to get factorial of any number

Java Program To Find Duplicate Values In an Array:

import java.util.*;

public class Codespeedy
{

     public static void main(String[] args) {
        int[] array= {1,2,3,4,5,3,6,1,87,87};
        int i;int k=0;
        List l1= new ArrayList();
       
        for(i=0;i<array.length;i++)
        {
        	
        	int temp=array[i];
        	for(int j=i+1;j<array.length;j++)
        	{
        	
        	if(temp==array[j])
        	{
        	
        
        			
                  l1.add(k,temp);
        	
        		   k++;
        		
        	}
        		
        	}
        }
      
        System.out.println(l1);
        
    }
}

Output:



[1, 3, 87]

Java Program To Check A Number is Palindrome or Not

We used List l1= new ArrayList();   here, we could take integer array but we didn’t. Just because we are not sure about the number of duplicate values, we are going to find. Instead of taking array we used List here because in List it is not mandatory to define the size of the list.

 

One response to “A Java Program To Find Duplicate Values In an Array”

  1. Sheli jesmin says:

    Easy to understand. Thanks for posting such a code

Leave a Reply

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