Skip to main content

Command Palette

Search for a command to run...

Sum of Unique Elements

Published
1 min read
Sum of Unique Elements
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 integer array nums. The unique elements of an array are the elements that appear exactly once in the array.

Return the sum of all the unique elements of nums.

LeetCode Problem - 1748

class Solution {
    public int sumOfUnique(int[] nums) {
        // Initialize a variable to store the sum of unique elements.
        int sum = 0;

        // Iterate through the array.
        for(int i=0; i<nums.length; i++){
            // Assume the current element is unique.
            boolean unique = true;

            // Iterate through the array again to check for duplicates.
            for(int j=0; j<nums.length; j++){
                // If the current element is equal to another element in the array and they are not at the same index,
                // then the current element is not unique.
                if(nums[i]==nums[j] && i!=j){
                    unique = false;
                    // Exit the inner loop as the uniqueness is already determined.
                    break;
                }
            }

            // If the current element is unique, add it to the sum.
            if(unique){
                sum +=nums[i];
            }
        }

        // Return the sum of unique elements.
        return sum;
    }
}

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.