Skip to main content

Command Palette

Search for a command to run...

Relative Sort Array

Published
2 min read
Relative Sort Array
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 arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.

Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that do not appear in arr2 should be placed at the end of arr1 in ascending order.

LeetCode Problem - 1122

class Solution {
    public int[] relativeSortArray(int[] arr1, int[] arr2) {
        // Initialize the result array to store the sorted elements
        int[] resultArray = new int[arr1.length];
        int idx = 0; // Index to track position in resultArray

        // Map to store frequency of each element in arr1
        Map<Integer, Integer> frequencyMap = new HashMap<>();

        // Count frequencies of elements in arr1
        for (int num : arr1) {
            frequencyMap.put(num, frequencyMap.getOrDefault(num, 0) + 1);
        }

        // Process elements from arr2 in the order they appear
        for (int num : arr2) {
            if (frequencyMap.containsKey(num)) {
                int count = frequencyMap.get(num);
                // Add num count times to resultArray
                while (count > 0) {
                    resultArray[idx++] = num;
                    count--;
                }
                // Remove num from frequencyMap as it's processed
                frequencyMap.remove(num);
            }
        }

        // Handle elements in arr1 that are not in arr2
        List<Integer> remainingElements = new ArrayList<>();
        for (int num : arr1) {
            if (frequencyMap.containsKey(num)) {
                remainingElements.add(num);
            }
        }

        // Sort remaining elements
        Collections.sort(remainingElements);

        // Add remaining elements to resultArray
        for (int num : remainingElements) {
            resultArray[idx++] = num;
        }

        // Return the sorted resultArray
        return resultArray;
    }
}

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.