Skip to main content

Command Palette

Search for a command to run...

Unique Number of Occurrences

Published
2 min read
Unique Number of Occurrences
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 an array of integers arr, return true if the number of occurrences of each value in the array is unique or false otherwise.

LeetCode Problem - 1207: Link | Click Here

import java.util.Arrays;
import java.util.ArrayList;

class Solution {
    public boolean uniqueOccurrences(int[] arr) {
        // Variable to store the final result
        boolean result = true;

        // Sort the input array in ascending order
        Arrays.sort(arr);

        // ArrayList to store the counts of consecutive identical elements
        ArrayList<Integer> al = new ArrayList<>();

        // Iterate through the sorted array
        for (int i = 0; i < arr.length; i++) {
            // Variable to count the occurrences of the current element
            int count = 0;

            // Count the occurrences of consecutive identical elements
            for (int j = i; j < arr.length; j++) {
                if (arr[i] == arr[j]) {
                    count++;
                } else {
                    // Break when a different element is encountered
                    break;
                }
            }

            // Add the count to the ArrayList
            al.add(count);

            // Skip the next elements in the outer loop since they are part of the same group
            i = i + count - 1;
        }

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

        // Sort the array of counts
        Arrays.sort(newArr);

        // Check if there are any repeated counts
        for (int i = 0; i < newArr.length - 1; i++) {
            if (newArr[i] == newArr[i + 1]) {
                // If there are repeated counts, set the result to false and break
                result = false;
                break;
            }
        }

        // Return the final result
        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.