Skip to main content

Command Palette

Search for a command to run...

Intersection of Two Arrays

Published
1 min read
Intersection of Two Arrays
G

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.

I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.

Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.

LeetCode Problem - 349

class Solution {
    // This method finds the intersection of two arrays `nums1` and `nums2`.
    public int[] intersection(int[] nums1, int[] nums2) {
        // ArrayList to store the intersection elements
        ArrayList<Integer> al = new ArrayList<>();

        // Nested loop to compare each element of nums1 with each element of nums2
        for (int num1 : nums1) {
            for (int num2 : nums2) {
                // If an element is found in both arrays and not already in the intersection list, add it to the list
                if (num1 == num2 && !(al.contains(num1))) {
                    al.add(num1);
                }
            }
        }

        // Convert the ArrayList to an array
        int[] ans = new int[al.size()];
        for (int i = 0; i < ans.length; i++) {
            ans[i] = al.get(i);
        }

        // Return the intersection array
        return ans;
    }
}

More from this blog

S

Software and Performance Testing Insights

462 posts

Results-Driven Agile QA Specialist | Expert in Mobile & Web Testing | Proficient in Test Planning, Execution, and Root Cause Analysis.