मराठी

Create two arrays STP[ ] and STQ[ ], both of type int and size 8 and fill them with variables. Create a third array STUN[ ] that contains all the elements from both the arrays without any repetition.

Advertisements
Advertisements

प्रश्न

Write a program for the following.

Create two arrays STP[ ] and STQ[ ], both of type int and size 8 and fill them with variables. Create a third array STUN[ ] that contains all the elements from both the arrays without any repetition.

For example:

Input STP[ ] 17 54 36 87 44 92 28 77        
Input STQ[ ] 72 94 44 33 77 87 17 37        
Output STUN[ ] 17 54 36 87 44 92 28 77 72 94 33 37
कोड लेखन
Advertisements

उत्तर

import java.util.Scanner;
public class UniqueUnion 
{
    public static void main(String[] args) 
    {
        Scanner sc = new Scanner(System.in);        
        int STP[] = new int[8];
        int STQ[] = new int[8];        
        int temp[] = new int[16];
        int uniqueCount = 0;        
        System.out.println("Enter 8 integers for array STP:");
        for (int j = 0; j < 8; j++) {
            STP[j] = sc.nextInt();            
            boolean isDuplicate = false;
            for (int k = 0; k < uniqueCount; k++) 
            {
                if (temp[k] == STP[j]) 
                {
                    isDuplicate = true;
                    break;
                }
            }
            if (!isDuplicate) 
            {
                temp[uniqueCount] = STP[j];
                uniqueCount++;
            }
        }        
        System.out.println("Enter 8 integers for array STQ:");
        for (int j = 0; j < 8; j++) {
            STQ[j] = sc.nextInt();            
            boolean isDuplicate = false;
            for (int k = 0; k < uniqueCount; k++) 
            {
                if (temp[k] == STQ[j]) 
                {
                    isDuplicate = true;
                    break;
                }
            }
            if (!isDuplicate) 
            {
                temp[uniqueCount] = STQ[j];
                uniqueCount++;
            }
        }        
        int STUN[] = new int[uniqueCount];
        for (int j = 0; j < uniqueCount; j++) 
        {
            STUN[j] = temp[j];
        }        
        System.out.print("\nSTUN[ ]\t");
        for (int j = 0; j < STUN.length; j++) 
        {
            System.out.print(STUN[j] + "\t");
        }        
        sc.close();
    }
}
shaalaa.com
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 7: Arrays - One Dimension - Exercises [पृष्ठ १७७]

APPEARS IN

रूपा पंडित Computer Applications [English] Class 10 ICSE
पाठ 7 Arrays - One Dimension
Exercises | Q 4. (vi) | पृष्ठ १७७
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×