Skip to main content

Command Palette

Search for a command to run...

Intersection of Two Arrays II

Published
1 min read
Intersection of Two Arrays II
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 appear as many times as it shows in both arrays and you may return the result in any order.

LeetCode Problem - 350

class Solution {
    // Method to find the intersection of two arrays
    public int[] intersect(int[] nums1, int[] nums2) {
        List<Integer> answerList = new ArrayList<>(); // List to store intersecting elements

        // Iterate through nums1 array
        for(int i=0; i<nums1.length; i++){
            int e = nums1[i]; // Current element from nums1
            // Iterate through nums2 array
            for(int j=0; j<nums2.length; j++){
                int k = nums2[j]; // Current element from nums2
                // If elements match and nums2 element is not marked as used (-1),
                // add to answerList and mark nums2 element as used
                if(e == k && nums2[j] != -1){
                    answerList.add(e);
                    nums2[j] = -1; // Mark nums2 element as used
                    break; // Move to the next element in nums1
                }
            }
        }

        // Convert answerList to an integer array
        int[] answer = new int[answerList.size()];
        for(int i=0; i<answer.length; i++){
            answer[i] = answerList.get(i); // Copy elements from answerList to answer array
        }

        return answer; // Return the intersection array
    }
}

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.