Answer to Question #304974 in Java | JSP | JSF for ram

Question #304974

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


1
Expert's answer
2022-03-03T09:58:45-0500
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");
            }
        }
    }

}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS