Task - Array Union - Merge Numbers #26
Replies: 46 comments 1 reply
-
|
`import java.util.Scanner; public class ArrayUnion { }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.*; |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
import java.util.Scanner; public class Count { |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.*; } |
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.*; public class Union { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; public class Unionsarray { } |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
|
`import java.util.Scanner; public class ArrayUnions { }` |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
public class ArrayUnion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
int[] arr1 = new int[n];
for(int i=0;i<n;i++){
arr1[i] = scanner.nextInt();
}
int[] arr2 = new int[m];
for(int i=0;i<n;i++){
arr2[i] = scanner.nextInt();
}
System.out.println(getUnionCount(getArr(arr1),getArr(arr2),n,m));
}
private static int[] getArr(int[] arr) {
Arrays.sort(arr);
int len = arr.length;
int j = 0;
for (int i = 0; i < len - 1; i++) {
if (arr[i] != arr[i + 1]) {
arr[j++] = arr[i];
}
}
arr[j++] = arr[len - 1];
return arr;
}
private static int getUnionCount(int[] arr1, int[] arr2,int n,int m) {
int i=0,j=0,count = 0;
while(i<n && j<m){
if(arr1[i] == arr2[j]) count++;
else count += 2;
i++;j++;
}
if(i<n) count += (n-i);
if(j<m) count += (m-j);
return count;
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
System.out.println("Enter the first array size");
int sizeOne = obj.nextInt();
System.out.println("Enter the second array size");
int sizeTwo = obj.nextInt();
int arrayOne[] = new int[sizeOne];
int arrayTwo[] = new int[sizeTwo];
int max = arrayOne.length + arrayTwo.length;
int checkArray[] = new int[max];
for (int iterate = 0; iterate < sizeOne; iterate++) {
System.out.println("Enter the first array element");
int element = obj.nextInt();
arrayOne[iterate] = element;
checkArray[iterate] = element;
}
int k = arrayOne.length;
for (int iter = 0; iter < sizeTwo; iter++) {
System.out.println("Enter the second array element");
int element = obj.nextInt();
arrayTwo[iter] = element;
checkArray[k] = element;
k++;
}
boolean flag = false;
int count = 0;
int count1 = 0;
for (int l = 0; l < checkArray.length; l++) {
for (int m = 0; m < checkArray.length; m++) {
if (checkArray[l] == checkArray[m] && !(l == m)) {
checkArray[l] = 0;
}
}
}
for (int z = 0; z < checkArray.length; z++) {
if (checkArray[z] != 0) {
count++;
}
}
System.out.println("The unique elements are: " + (count));
}
} |
Beta Was this translation helpful? Give feedback.
-
TASK-ARRAY UNION-MERGE NUMBERS1.Merge.javapackage Arrays;
import java.util.Scanner;
public class Merge {
public static int union(int[] arr1, int[] arr2,int a,int b) {
System.out.println("Union of two array :");
int count=0;
int i = 0, j = 0;
while (i < a && j < b) {
if (arr1[i] < arr2[j])
{
System.out.print(arr1[i++] + " ");
count++;
i++;}
else if (arr2[j] < arr1[i])
{
System.out.print(arr2[j++] + " ");
count++;
j++;}
else {
System.out.print(arr2[j++] + " ");
count++;
i++;
j++;
}
}
while (i < a)
System.out.print(arr1[i++] + " ");
count++;
i++;
while (j < b)
System.out.print(arr2[j++] + " ");
count++;
j++;
System.out.println("\ncount of union array :"+count);
return count;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int count=0;
System.out.println("Enter the size of array 1");
int a=sc.nextInt();
System.out.println("Enter the size of array 2");
int b=sc.nextInt();
int arr1[]=new int[a];
int arr2[]=new int[b];
System.out.println("Enter the elements of array 1");
for(int i=0;i<a;i++)
{
arr1[i]=sc.nextInt();
}
System.out.println("Enter the elements of array 2");
for(int i=0;i<b;i++)
{
arr2[i]=sc.nextInt();
}
union(arr1,arr2,a,b);
}
}Output |
Beta Was this translation helpful? Give feedback.
-
1. UnionArrayimport java.util.Scanner;
public class UnionArray{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the array1 elements");
int array1[]=new int[5];
for(int i=0; i<5; i++){
array1[i] =scanner.nextInt();
}
System.out.println("Enter the array2 elements");
int array2[]=new int[4];
for(int i=0; i<4; i++){
array2[i]=scanner.nextInt();
}
System.out.println("The array1 elemnts");
for (int row: array1){
System.out.println(row);
}
System.out.println("The array2 elemnts");
for (int row: array2){
System.out.println(row);
}
findUnion(array1,array2);
}
public static void findUnion(int[] array1, int[] array2) {
int i=0;
int j=0;
while (i < array1.length && j < array2.length) {
if (array1[i] < array2[j]) {
System.out.print(array1[i] + " ");
i++;
} else if (array2[j] < array1[i]) {
System.out.print(array2[j] + " ");
j++;
} else {
System.out.print(array1[i] + " ");
i++;
j++;
}
}
while (i < array1.length) {
System.out.print(array1[i] + " ");
i++;
}
while (j < array2.length) {
System.out.print(array2[j] + " ");
j++;
}
}
} |
Beta Was this translation helpful? Give feedback.
-
1public class UnionArray {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int num1;
int num2;
System.out.println("Enter array1 size ");
num1 = scanner.nextInt();
System.out.println("Enter array2 size ");
num2 = scanner.nextInt();
int arr1[] = new int[num1];
int arr2[] = new int[num2];
int arr3[] = new int[num1 + num2];
int count = 0;
System.out.println("enter array1 element ");
for (int i = 0; i < arr1.length; i++) {
arr1[i] = scanner.nextInt();
}
System.out.println("Enter array2 element ");
for (int j = 0; j < arr2.length; j++) {
arr2[j] = scanner.nextInt();
}
for (int i = 0; i < arr1.length; i++) {
arr3[i] = arr1[i];
}
int len = arr1.length;
for (int i = 0; i < arr2.length; i++) {
arr3[len + i] = arr2[i];
}
for (int j = 0; j < arr3.length - 1; j++) {
for (int k = j + 1; k < arr3.length; k++) {
if (arr3[j] == arr3[k]) {
arr3[k] = 0;
}
}
}
for (int i = 0; i < arr3.length; i++) {
if (arr3[i] > 0) {
count++;
}
}
System.out.println("Union count " + count);
scanner.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Merge Arrayimport java.util.Scanner;
public class MergeArrays{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int arr1[]=new int[10];
int arr2[]=new int[10];
int same=0;
System.out.println("Enter length of 2 Arrays: ");
int a1=sc.nextInt();
int a2=sc.nextInt();
System.out.println("Enter the Array Elements:");
System.out.println("First Array");
for(int i=0; i<a1 ; i++){
arr1[i]=sc.nextInt();
}
System.out.println("Second Array");
for(int i=0; i<a2 ; i++){
arr2[i]=sc.nextInt();
}
for(int i=0;i<a1;i++){
for(int j=0;j<a2;j++){
if(arr1[i]==arr2[j]){same++;}
}
}
int count=(a1+a2)-same;
System.out.println("count = "+count);
sc.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
Merging of Arrayimport java.util.*;
public class ArrayMerge {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter size of 1st array : ");
int m = sc.nextInt();
System.out.println("Enter size of 2nd array : ");
int n = sc.nextInt();
int[] arr1 = new int[m];
int[] arr2 = new int[n];
System.out.println("Enter the elements of 1st array : ");
for (int i = 0; i < m; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("Enter the elements of 2nd array : ");
for (int i = 0; i < n; i++) {
arr2[i] = sc.nextInt();
}
System.out.println("Sum of Union of array is : ");
int i = 0;
int j = 0;
int sum = 0;
while (i < m && j < n) {
if (arr1[i] < arr2[j]) {
sum++;
i++;
}
else if (arr2[j] < arr1[i]) {
sum++;
j++;
} else {
sum++;
i++;
j++;
}
}
if (i < m) {
while (i < m) {
sum++;
i++;
}
}
if (j < n) {
while (j < n) {
sum++;
j++;
}
}
System.out.println(sum);
}
} |
Beta Was this translation helpful? Give feedback.
-
ArrayUnion_MergeNumberspackage Array.SingleDimensionalArray;
import java.util.Scanner;
class Union {
public static int union(int a[], int b[]) {
int i=0, j=0,count=0;
int n=a.length;
int m=b.length;
while (i<n && j<m){
if (a[i] == b[j])
count++;
else
count+=2;
i++;
j++;
}
if (i<n)
return count + (n-i);
else
return count + (m-j);
}
}
public class ArrayUnion {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter size of Array 1: ");
int n = Integer.parseInt(scanner.nextLine());
System.out.print("Enter size of Array 2: ");
int m = Integer.parseInt(scanner.nextLine());
int a[] = new int[n];
int b[] = new int[m];
System.out.println("Enter Array 1 elements: ");
for (int i=0; i<n; i++){
a[i] = Integer.parseInt(scanner.nextLine());
}
System.out.println("Enter Array 2 elements: ");
for (int i=0; i<m; i++){
b[i] = Integer.parseInt(scanner.nextLine());
}
System.out.println("Number Count: " + Union.union(a, b));
scanner.close();
}
}Output |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class Union {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of 1st array:");
int a = sc.nextInt();
System.out.println("Enter the size of 2nd array:");
int b = sc.nextInt();
int arr1[] = new int[a];
System.out.println("Enter the elements of 1st array:");
for (int i = 0; i < a; i++) {
arr1[i] = sc.nextInt();
}
int arr2[] = new int[b];
System.out.println("Enter the elements of 2nd array:");
for (int i = 0; i < b; i++) {
arr2[i] = sc.nextInt();
}
int cnt = 0;
int sum = a + b;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (arr1[i] == arr2[j]) {
sum--;
}
}
}
System.out.println("Output: " + sum);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Arrays;
import java.util.Scanner;
public class discussion26 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter sizes of two arrays: ");
int m = sc.nextInt();
int n = sc.nextInt();
int[] arr1 = new int[m];
int[] arr2 = new int[n];
System.out.println("Enter elements of array 1");
for (int i = 0; i < m; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("Enter elements of array 2");
for (int i = 0; i < n; i++) {
arr2[i] = sc.nextInt();
}
Arrays.sort(arr1);
Arrays.sort(arr2);
int i = 0, j = 0;
int count = 0;
while (i < m && j < n) {
if (arr1[i] < arr2[j]) {
count++;
i++;
} else if (arr2[j] < arr1[i]) {
count++;
j++;
} else {
count++;
i++;
j++;
}
}
while (i < m) {
count++;
i++;
}
while (j < n) {
count++;
j++;
}
System.out.println(count);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
public class d26 {
public static void main(String[] args) {
Scanner obj = new Scanner(System.in);
int a[] = new int[10];
int b[] = new int[10];
int same = 0;
System.out.println("Enter length of 2 Arrays: ");
int n = obj.nextInt();
int m = obj.nextInt();
System.out.println("Enter the Array Elements:");
System.out.println("First Array");
for (int i = 0; i < n; i++) {
a[i] = obj.nextInt();
}
System.out.println("Second Array");
for (int i = 0; i < m; i++) {
b[i] = obj.nextInt();
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (a[i] == b[j]) {
same++;
}
}
}
int count = (m + n) - same;
System.out.println("count = " + count);
obj.close();
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class D26 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter the size of 1st array:");
int a = sc.nextInt();
System.out.println("Enter the size of 2nd array:");
int b = sc.nextInt();
int arr1[] = new int[a];
int arr2[] = new int[b];
System.out.println("Enter the elements of 1st array:");
for (int i = 0; i < a; i++) {
arr1[i] = sc.nextInt();
}
System.out.println("Enter the elements of 2nd array:");
for (int i = 0; i < b; i++) {
arr2[i] = sc.nextInt();
}
int sum = a + b;
for (int i = 0; i < a; i++) {
for (int j = 0; j < b; j++) {
if (arr1[i] == arr2[j]) {
sum--;
}
}
}
System.out.println("Output: " + sum);
}
} |
Beta Was this translation helpful? Give feedback.
-
|
Beta Was this translation helpful? Give feedback.
-
import java.util.Scanner;
public class Union {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n, m;
System.out.println("Enter the n and m");
n = sc.nextInt();
m = sc.nextInt();
int[] a = new int[n];
int[] b = new int[m];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
for (int i = 0; i < m; i++) {
b[i] = sc.nextInt();
}
int count = 0;
for (int i = 0; i < n; i++) {
int flag = 0;
for (int j = 0; j < m; j++) {
if (a[i] == b[j] && b[j] != 0) {
count++;
b[j] = 0;
flag = 1;
}
}
if (flag == 0) {
count++;
}
}
System.out.println(count);
}
} |
Beta Was this translation helpful? Give feedback.
-
import java.util.*;
public class task26 {
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int arr1[] = new int[10];
int arr2[] = new int[10];
int cnt = 0;
System.out.println("enter length of array 1");
int l1 = scn.nextInt();
System.out.println("enter length of array 2");
int l2 = scn.nextInt();
System.out.println("enter elements of the array");
System.out.println("for the 1st array");
for (int i = 0; i < l1; i++) {
arr1[i] = scn.nextInt();
}
System.out.println("for the 2nd array");
for (int i = 0; i < l2; i++) {
arr2[i] = scn.nextInt();
}
for (int i = 0; i < l1; i++) {
for (int j = 0; j < l2; j++) {
if (arr1[i] == arr2[j]) {
cnt++;
}
}
}
int tot = (l1 + l2) - cnt;
System.out.println("total count = " + tot);
}
} |
Beta Was this translation helpful? Give feedback.


Uh oh!
There was an error while loading. Please reload this page.
-
Given two arrays a[] and b[] of size n and m respectively. The task is to find the union between these two arrays.
Union of the two arrays can be defined as the set containing distinct elements from both the arrays. If there are repetitions, then only one occurrence of element should be printed in the union.
Example 1:
Example 2:
Beta Was this translation helpful? Give feedback.
All reactions