How to Find Duplicates in Array in Java


In this example, let’s  see how to check duplicated value in an array in Java.

Source Code

package com.beginner.examples;

public class ArrayRepeatExample {

    public static void main(String[] args){
         int[] arr={23,325,27,23,48};
    	 boolean flag=false;
    	 for (int i = 0; i < arr.length; i++) 
    	 {
	    	  int tem=arr[i];
	    	  int num=0;
	    	  for (int j = 0; j =2)
	    	  {
	    		  flag=true;
	    	  }
    	 }
    	 if(flag)
    	 {
    		 System.out.println("Value is duplicated!");
    	 }
    	 else
    	 {
    		 System.out.println("Value is not duplicated!");
    	 }
    }
}

Output:

Value is duplicated!
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments