Find duplicates in array in one iteration in java


In this core java programming tutorial we will write a program to Find subArray in array whose sum matches required sum in java.


Write a program to find out duplicates in array in one iteration.



Example/ Full Program/SourceCode to Find duplicates in array in one iteration in java >
/** Copyright (c), AnkitMittal  JavaMadeSoEasy.com */
public class FindDuplicateInOneIterationExample{
   static int ar[]={3,7,3,5,8,9,24,8,9}; //given array
  
   public static void main(String[] args) {
          System.out.print("array : ");
          for (int j = 0; j < ar.length; j++)
                 System.out.print(ar[j] +" "); // display it
         
          displayDuplicateInOneIteration();    
   }
  
   /*
   * Method for displaying duplicate in one iteration
   */
   static public void displayDuplicateInOneIteration(){
         
          int tempAr[]=new int[100];
          System.out.print("\nduplicates :  ");
          for (int j = 0; j < ar.length; j++){ /*make indexes of tempAr corresponding to value found in ar equal to 1.
                                                                              i.e if i[0]=22, than make tempAr[22]=1;
                                                                              if i[1]=25, than make tempAr[25]=1; */
                 if(tempAr[ar[j]]==0){
                       tempAr[ar[j]]=1;
                 }
                 else{
                       System.out.print(ar[j]+" ");
                 }
          }  
   }  
  
}
/*OUTPUT
array : 3 7 3 5 8 9 24 8 9
duplicates :  3 8 9
*/



Previous program                                                                  Next program


So in this core java programming tutorial we wrote a program how to Find subArray in array whose sum matches required sum in java.


Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy.com (JMSE) on facebook, following on google+ or Twitter.




RELATED LINKS>









No comments:

Post a Comment