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 [पृष्ठ १७७]
