KnowledgeBoat Logo

Computer Science

Write a program to input N and M number of names in two different single dimensional arrays A and B respectively, such that none of them have duplicate names. Merge the arrays A and B into a single array C, such that the resulting array is sorted alphabetically. Display all the three arrays.

Test your program for the following data and some random data:

Sample data:

Input:
Enter the names in array A, N = 2
Enter the names in array B, M = 3
First array: A
Suman
Anil
Second array: B
Usha
Sachin
John

Output:
Sorted Merged array: C
Anil
John
Sachin
Suman
Usha
Sorted First array: A
Anil
Suman
Sorted Second array: B
John
Sachin
Usha

Java

Java Arrays

7 Likes

Answer

import java.util.Scanner;

public class KboatSDANames
{
    public static void main(String args[]) {
        Scanner in = new Scanner(System.in);
        System.out.print("Enter the names in array A, N = ");
        int n = in.nextInt();
        System.out.print("Enter the names in array B, M = ");
        int m = in.nextInt();
        in.nextLine();
        
        String a[] = new String[n];
        String b[] = new String[m];
        String c[] = new String[m+n];
        
        System.out.println("First array: A");
        for (int i = 0; i < n; i++) {
            String name = in.nextLine();
            for (int j  = i - 1; j >= 0; j--) {
                if (a[j].equalsIgnoreCase(name)) {
                    System.out.println(name + " already present in array");
                    return;
                }    
            }
            
            a[i] = name;
        }
        
        System.out.println("Second array: B");
        for (int i = 0; i < m; i++) {
            String name = in.nextLine();
            
            for (int k = 0; k < n; k++) {
                if (a[k].equalsIgnoreCase(name)) {
                    System.out.println(name + " already present in array");
                    return;
                }
            }
            
            for (int j = i - 1; j >= 0; j--) {
                if (b[j].equalsIgnoreCase(name)) {
                    System.out.println(name + " already present in array");
                    return;
                }  
            }
            
            b[i] = name;
        }
        
        sortArray(a);
        sortArray(b);
        int aIdx = 0, bIdx = 0;
        
        //Merge the arrays preserving the sorting
        for (int i = 0; i < m+n; i++) {            
            if (aIdx == n || a[aIdx].compareToIgnoreCase(b[bIdx]) > 0) {
                c[i] = b[bIdx];
                bIdx++;
            }
            else if (bIdx == m || b[bIdx].compareToIgnoreCase(a[aIdx]) > 0) {
                c[i] = a[aIdx];
                aIdx++;
            }
        }
        
        System.out.println("Sorted Merged array: C");
        printArray(c);
        
        System.out.println("Sorted First array: A");
        printArray(a);
        
        System.out.println("Sorted Second array: B");
        printArray(b);
    }
    
    public static void sortArray(String arr[]) {
        for (int i = 0; i < arr.length; i++) {
            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j - 1].compareToIgnoreCase(arr[j]) > 0) {
                    String t = arr[j - 1];
                    arr[j - 1] = arr[j];
                    arr[j] = t;
                }
            }  
        }
    }
    
    public static void printArray(String arr[]) {
        for (int i = 0; i < arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}

Output

BlueJ output of Write a program to input N and M number of names in two different single dimensional arrays A and B respectively, such that none of them have duplicate names. Merge the arrays A and B into a single array C, such that the resulting array is sorted alphabetically. Display all the three arrays. Test your program for the following data and some random data: Sample data: Input: Enter the names in array A, N = 2 Enter the names in array B, M = 3 First array: A Suman Anil Second array: B Usha Sachin John Output: Sorted Merged array: C Anil John Sachin Suman Usha Sorted First array: A Anil Suman Sorted Second array: B John Sachin Usha

Answered By

3 Likes


Related Questions