Title Description:
Merges two integer arrays in ascending order and filters out duplicate array elements
Input:
Enter a description, entered in the following order:
1 Enter the number of the first array
2 Enter a numeric value for the first array
3 Enter the number of the second array
4 Enter a value for the second array
Output:
Array after the output is merged
Input Sample:
3 1 2 5 4-1 0 3 2
Sample output:
-101235
Ideas:
First, merge the two arrays into one, then sort the entire array, and finally reject the duplicated data and output
1 ImportJava.util.Scanner;2 3 Public classArraymerge {4 5 Public Static voidMain (string[] args) {6 //Input , enter two arrays of data according to the input criteria7Scanner cin =NewScanner (system.in); 8 intLen1 =cin.nextint ();9 if(Len1 < 1){TenSystem.exit (-1); One } A int[] arr1 =New int[len1]; - for(inti = 0; i < len1; i++){ -Arr1[i] =cin.nextint (); the } - intLen2 =cin.nextint (); - if(Len2 < 1){ -System.exit (-1); + } - int[] arr2 =New int[len2]; + for(inti = 0; i < len2; i++){ AArr2[i] =cin.nextint (); at } - cin.close (); - - System.out.print (INTARRAYMERGEASC (ARR1,ARR2)); - } - in /** - * Merge and then store the output in a string to return to * @paramarr1 + * @paramarr2 - * @return the */ * Private StaticString INTARRAYMERGEASC (int[] arr1,int[] arr2) { $ //merges two arrays into a larger arrayPanax Notoginseng int[] Total =New int[Arr1.length +Arr2.length]; - for(inti = 0; i < arr1.length; i++){ theTotal[i] =Arr1[i]; + } A for(inti = 0; i < arr2.length; i++){ theTotal[i+arr1.length] =Arr2[i]; + } - //to sort a merged array $ sort (total); $ //saved in the result in a string -String res = total[0]+ "" ; - intLast = Total[0] ; the for(inti = 1; i < total.length; i++){ - //not the same is added to the string, the same is the next directWuyi if(Total[i]! =Last ) { theLast =Total[i]; -Res + = Total[i] + "" ; Wu } - } About $ returnRes; - - } - A /** + * Simple selection sorting algorithm for sorting arrays the * @paramarr - */ $ Private Static voidSortint[] arr) { the intmin; the intindex; the for(inti = 0; i < arr.length-1; i++){ theMin =Arr[i]; -index =i; in for(intj = i+1; J < Arr.length; J + +){ the if(Min >Arr[j]) { theMin =Arr[j]; Aboutindex =J; the } the } the if(Index! =i) { +Arr[index] =Arr[i]; -Arr[i] =min; the }Bayi } the } the -}
Code
OJ Platform--shaping array merging