DEV Community

Codes With Pankaj
Codes With Pankaj

Posted on

Question: Find the Common Elements in Two Arrays

Question: Find the Common Elements in Two Arrays

Problem Description:
Given two arrays of integers, write a Java function to find and print the common elements present in both arrays.

Solution:

import java.util.HashSet;

public class CommonElementsInArrays {
    // Function to find common elements in two arrays
    static void findCommonElements(int[] arr1, int[] arr2) {
        HashSet<Integer> set = new HashSet<>();
        for (int num : arr1) {
            set.add(num);
        }

        System.out.print("Common elements: ");
        for (int num : arr2) {
            if (set.contains(num)) {
                System.out.print(num + " ");
            }
        }
    }

    public static void main(String[] args) {
        int[] arr1 = {1, 2, 4, 5, 6};
        int[] arr2 = {2, 3, 5, 7};

        System.out.println("Array 1: [1, 2, 4, 5, 6]");
        System.out.println("Array 2: [2, 3, 5, 7]");

        findCommonElements(arr1, arr2);
    }
}
Enter fullscreen mode Exit fullscreen mode

Explanation:
This solution uses a HashSet to store unique elements from the first array (arr1). Then, it iterates through the second array (arr2) and checks if each element is present in the HashSet. If it is, the element is a common element, and it is printed.

This question assesses the candidate's knowledge of data structures (HashSet in this case) and their ability to efficiently solve problems involving arrays and sets.

AWS Security LIVE!

Join us for AWS Security LIVE!

Discover the future of cloud security. Tune in live for trends, tips, and solutions from AWS and AWS Partners.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay