Develop a java program to get n numbers as input and find the second duplicate and print the reverse of a number
First line contains the value n and the remaining lines contain the corresponding input
Input:
5
11
24
33
11
24
Output:
42
Note:
If the second duplicate is single digit, print as it is
If there is no second duplicate, print the first duplicate itself
If there is no duplicate, print "No duplicate found
package com.task;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Integer cnt = Integer.valueOf(sc.next());
Integer[] arr = new Integer[cnt];
for (int i = 0; i < cnt; i++) {
arr[i] = Integer.valueOf(sc.next());
}
Integer firstDup = null;
Integer secondDup = null;
firstloop:
for (int i = 0; i < arr.length; i++) {
for (int j = i+1; j < arr.length; j++) {
if (arr[i].equals(arr[j])) {
if (firstDup == null) {
firstDup = arr[i];
} else {
secondDup = arr[i];
break firstloop;
}
}
}
}
if (secondDup != null) {
StringBuilder sb = new StringBuilder(secondDup.toString());
sb.reverse();
System.out.println(sb.toString());
} else {
if (firstDup != null) {
System.out.println(firstDup);
} else {
System.out.println("No duplicate found");
}
}
}
}
Comments
Leave a comment