Write a program that returns a list containing only the common elements between two lists. For example, given the lists [1, 2, 3, 4] and [3, 4, 5, 6], the program should return [3, 4]. what u think about my sulution

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            int array1_size;
            int array2_size;
            int[] common_elements = new int[100];
            int common_count = 0;

            do {
                    System.out.println("Enter first array size (1-100):");
                    array1_size = scanner.nextInt();
            } while (array1_size <= 0 && array1_size > 100);

            do {
                    System.out.println("Enter second array size (1-100):");
                    array2_size = scanner.nextInt();
            } while (array2_size <= 0 && array2_size > 100);

            int[] array1 = new int[array1_size];
            int[] array2 = new int[array2_size];

            System.out.println("Enter elements for the first array:");
            for (int i = 0; i < array1_size; i++) {
                    array1[i] = scanner.nextInt();
            }

            System.out.println("Enter elements for the second array:");
            for (int i = 0; i < array2_size; i++) {
                    array2[i] = scanner.nextInt();
            }

            for (int i = 0; i < array1_size; i++) {
                    int j = 0;
                    boolean found = false;
                    while (j < array2_size && !found) {
                            if (array1[i] == array2[j]) {
                                    found = true;
                                    common_elements[common_count++] = array1[i];
                            }
                            j++;
                    }
            }

            System.out.println("Common elements:");
            for (int i = 0; i < common_count; i++) {
                    System.out.print(common_elements[i] + " ");
            }
    }

}

Greetings.

  1. Remember that less is more. When you want to create a system or program, resist the urge to be clever or “complete”, and just create a program that does strictly what you want it to do. In this case, that is to compare two arrays, evaluate the elements in the array, and then create a new array with common elements.
  2. Create variables and functions (methods that only do one thing/return a value) when you need them. For example, inside the method where they will be used. This cleans your code, prevents errors, and makes it easier for someone reading your code to understand what each variable, function, and methods do.
  3. Avoid using print statements unless needed. Following rule number two will help you do this. This slows down your code and may cause other issues with memory when you develop a major program where you will need to lock, pause, or otherwise manage threads.
  4. Minimize the use of recursion. The first and second rules will again help you achieve this. Use recursion only when it is needed.
  5. Create a class for each major method, this way your code is not linear, and you may decouple (ironically) the chances of creating difficult-to-find errors. This way when one element or method is changed the rest of your code is safeguarded. This is a way of “Programming in the Small” and saving countless hours of debugging.
  6. Programming in the small will help you visualize how your code can be expanded or reused in another project. Try it. This is also called “Programming from the bottom up”.

I challenge you to re-write your code with less lines, and please do not feel disappointed or defeated by my critique of your code. This is what programming is, finding your answers to a challenge and learning to fine-tune that answer more and more. 90% of your experience may become reading someone else’s code. So keep coding and recoding, so you can memorize as many techniques as possible before you get paid for reading someone else’s code for the most part.

Challenge accepted! I’ll do my best.

Here, I recommend you check this similar post. Follow the link below. It will direct you to some great videos about coding paradigms and software design. These are my favorie all time YouTube coding channels. Click on the name of the channels to visit the videos that I recommended to the other user. And do not feel overwhelmed. Watch those videos patiently, your coding style will immediately change.