[Solved] Find duplicate number in array and print the last duplicate number only [closed]


Simple way is iterate and compare it. like below, I hope it may solve your problem

 int[] a = {1,2,2,3,4,3,5,5,7};
 String b = "";
 int count=0;
 for(int i = 0; i<a.length;i++){
     for(int j=0; j<a.length;j++){
         if(a[i]==a[j]){
             count++;
         }
     }
     if(count>1){
         count=0;
         b = String.valueOf(a[i]);
     }else{
         count = 0;
     }
 }
  System.out.println("last duplicate value : "
                       + b);

Java 8 feature same solution

String output="N/A";
              Integer[] a = {1,2,7,2,3,4,3,5,5,7};
               List<Integer> list= Arrays.asList(a);

              list= list.stream().map(m->m).sorted(Comparator.reverseOrder()).collect(Collectors.toList());
              List<Integer> listSample=list;
               Optional<Integer> data = list.stream().filter(m->listSample.stream().filter(c->c==m).count()>1).findFirst();
              if(data.isPresent()) {
                  output=  data.get().toString();
              }

1

solved Find duplicate number in array and print the last duplicate number only [closed]