Skip to main content

Command Palette

Search for a command to run...

Count Elements With Maximum Frequency

Published
1 min read
Count Elements With Maximum Frequency
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.

You are given an array nums consisting of positive integers.

Return the total frequencies of elements in nums such that those elements all have the maximum frequency.

The frequency of an element is the number of occurrences of that element in the array.

LeetCode Problem - 3005

class Solution {
    public int countWords(String[] words1, String[] words2) {
        // Variable to store the count of words appearing exactly once in both arrays
        int result = 0;
        // HashMap to store the frequency of words in words1
        HashMap<String, Integer> hm1 = new HashMap<>();
        // HashMap to store the frequency of words in words2
        HashMap<String, Integer> hm2 = new HashMap<>();

        // Count the frequency of words in words1
        for (String str : words1) {
            hm1.put(str, hm1.getOrDefault(str, 0) + 1);
        }
        // Count the frequency of words in words2
        for (String str : words2) {
            hm2.put(str, hm2.getOrDefault(str, 0) + 1);
        }

        // Iterate through each word in words1
        for (String word : words1) {
            // If the word appears exactly once in both words1 and words2, increment the result
            if (hm1.get(word) == 1 && hm2.getOrDefault(word, 0) == 1) {
                result++;
            }
        }

        // Return the count of words appearing exactly once in both arrays
        return result;
    }
}

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.