Two arrays are given. It is necessary to calculate how many numbers are contained simultaneously in both the first and second arrays and output them in ascending order.

Note: If there is a number in the first array (for example, 1), and in the second array this number occurs twice (for example, an array of 5 elements: 1,2,3,4,1 ), then in this case the number 1 is 1 times in both arrays. If the number occurs several times in each array, then it is still one match.

My code is:

 package shestnadcat; import java.util.Scanner; public class Shestnadcat2 { public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ количСство элСмСнтов ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ массива"); int b = in.nextInt(); int i; int[] a = new int[b]; for (i = 0; i < b; i++) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт " + i); a[i] = in.nextInt(); } System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ количСство элСмСнтов Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ массива"); int d = in.nextInt(); int[] c = new int[d]; for (i = 0; i < d; i++) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт " + i); c[i] = in.nextInt(); } int k = 0; for (i = 0; i < b; i++) { int g = a[i]; for (i = 0; i < d; i++) { if (g == c[i]) { k = +1; } } } System.out.println(k); } } 

enter image description here

The counter incorrectly displays the result.

I can not think of the output of these numbers.

  • Π‘Ρ‡Ρ‘Ρ‚Ρ‡ΠΈΠΊ Π½Π΅ΠΏΡ€Π°Π²ΠΈΠ»ΡŒΠ½ΠΎ Π²Ρ‹Π²ΠΎΠ΄ΠΈΡ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚. - and what? - Alexey Shimansky
  • You have counter i in two cycles, but it should be different .... standard i and j - Alexey Shimansky
  • @ Alexey Shimansky result has not changed. Screenshot attach to the post itself - Marat Zimnurov
  • @ Alexey Shimansky pinned - Marat Zimnurov
  • one
    In arrays {1, 1, 1} and {1, 1, 1} how many identical numbers? - andy.37

4 answers 4

 public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] firstArray = readArray(in, "ΠΏΠ΅Ρ€Π²ΠΎΠ³ΠΎ"); int[] secondArray = readArray(in, "Π²Ρ‚ΠΎΡ€ΠΎΠ³ΠΎ"); //созданиС массива ΠΎΡ‚ΠΌΠ΅Ρ‚ΠΎΠΊ ΠΎ Π½Π°ΠΉΠ΄Π΅Π½Π½Ρ‹Ρ… совпадСниях Π²ΠΎ Π²Ρ‚ΠΎΡ€ΠΎΠΌ массивС boolean[] secondMatches = new boolean[secondArray.length]; for (int i = 0; i < secondMatches.length; i++) { secondMatches[i] = false; } int matchesCount = 0; //поиск совпадСний for (int firstElement : firstArray) { for (int i = 0; i < secondArray.length; i++) { if (firstElement == secondArray[i]) { if (!secondMatches[i]) { secondMatches[i] = true; matchesCount++; } break; } } } //созданиС массива совпадСний int[] matches = new int[matchesCount]; int k = 0; for (int i = 0; i < secondArray.length; i++) { if (secondMatches[i]) { matches[k++] = secondArray[i]; } } //сортировка ΠΏΡƒΠ·Ρ‹Ρ€ΡŒΠΊΠΎΠΌ массива совпадСний for (int i = matches.length - 1; i >= 0; i--) { for (int j = 0; j < i; j++) { if (matches[j] > matches[j + 1]) { int swap = matches[j]; matches[j] = matches[j + 1]; matches[j + 1] = swap; } } } System.out.println(matchesCount); for (int element : matches) { System.out.print(element + " "); } } private static int[] readArray(Scanner in, String arrayNumberName) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ количСство элСмСнтов " + arrayNumberName + " массива"); int elementsCount = in.nextInt(); int[] elements = new int[elementsCount]; for (int i = 0; i < elementsCount; i++) { System.out.println("Π’Π²Π΅Π΄ΠΈΡ‚Π΅ элСмСнт " + i); elements[i] = in.nextInt(); } return elements; } 
  • Great!) Thank you!) - Marat Zimnurov
  • @ MaratZimnurov health. And, by the way, try to use meaningful variable names in the future and not save on spaces. It helps a lot in programming. - Regent

It is necessary to shove all the values ​​in the HashMap and compare the size of the HashMap with the number of entries in both arrays:

 int[] firstArray = new int[]{1, 3, 4, 5, 56, 6}; int[] secondArray = new int[]{11, 8, 3, 4, 5, 56, 6}; HashMap hashmap=new HashMap<String, Integer>(); for(int i=0; i < firstArray.length; i++) hashmap.put(new Integer(firstArray[i]).toString(), new Integer(firstArray[i])); for(int i=0; i < secondArray.length; i++) hashmap.put(new Integer(secondArray[i]).toString(), new Integer(secondArray[i])); System.out.println("Π‘ΠΎΠ²ΠΏΠ°Π΄Π°ΡŽΡ‰ΠΈΡ… Π·Π½Π°Ρ‡Π΅Π½ΠΈΠΉ="+(firstArray.length+secondArray.length)-hashmap.size())); 
     import java.util.*; public class Main { public static void main(String[] args) { ArrayList<Integer> first = new ArrayList<Integer>(); ArrayList<Integer> second = new ArrayList<Integer>(); first.add(1); first.add(2); first.add(3); first.add(42); second.add(3); second.add(4); second.add(5); second.add(42); second.retainAll(first); Collections.sort(second); System.out.println(second); } } 

    Visually see .

    • no no no. will not work. The author has primitives - Alexey Shimansky
    • @ Alexey Shimansky, which may be altered under the lists in order to work as a white person. - user207618
    • I did not understand anything from the code ... I am in primitives (can you simplify? - Marat Zimnurov
    • @MaratZimnurov with primitives is just the opposite - there will be a lot of code. - Alexey Shimansky
    • @ Alexey Shimansky is not important (the main thing is to be) - Marat Zimnurov

    Option with stream api:

     int[] firstArray = new int[]{1, 3, 4, 5, 56, 6}; int[] secondArray = new int[]{11, 8, 3, 4, 5, 56, 6}; IntStream .concat(Arrays.stream(firstArray), Arrays.stream(secondArray)) .distinct() .sorted() .forEach(System.out::println); 

    A little confused and wrote a small implementation of streams:

     private static class CustomIntStream { private final int[] array; public CustomIntStream(int[] array) { this.array = array; } @Override public String toString() { return Arrays.toString(array); } public static CustomIntStream of(int[] firstArray, int[] secondArray) { int[] array = new int[firstArray.length + secondArray.length]; System.arraycopy(firstArray, 0, array, 0, firstArray.length); System.arraycopy(secondArray, 0, array, firstArray.length, secondArray.length); return new CustomIntStream(array); } public CustomIntStream distinct() { int[] buffer = new int[array.length]; int index = 0; for (int i = 0; i < array.length; i++) { boolean isContains = false; for (int j = i + 1; j < array.length; j++) if (isContains = (array[i] == array[j])) break; if (!isContains) buffer[index++] = array[i]; } int[] newArray = new int[index]; System.arraycopy(buffer, 0, newArray, 0, newArray.length); return new CustomIntStream(newArray); } public CustomIntStream sorted() { int[] newArray = new int[array.length]; System.arraycopy(array, 0, newArray, 0, array.length); sort(newArray, 0, newArray.length - 1); return new CustomIntStream(newArray); } private static void sort(int[] array, int start, int end) { int middle = (end - start) / 2 + start; int left = start; int right = end; while (left < right) { while (left <= end && array[middle] > array[left]) left++; while (right >= start && array[middle] < array[right]) right--; if (left <= right) { int buffer = array[left]; array[left] = array[right]; array[right] = buffer; right--; left++; } } if (start < right) sort(array, start, right); if (left < end) sort(array, left, end); } public void foreach(IntConsumer consumer) { for (int i = 0; i < array.length; i++) consumer.accept(array[i]); } } 

    Used similarly:

     CustomIntStream .of(firstArray, secondArray) .distinct() .sorted() .foreach(System.out::println); 
    • I would like to see primitives ( - Marat Zimnurov
    • gorgeous) thank) - Marat Zimnurov