Skip to main content

Command Palette

Search for a command to run...

Find Unique Number in Array

Published
1 min read
Find Unique Number in 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 a non-empty array of integers nums, every element appears twice except for one. Find that single one.

You must implement a solution with a linear runtime complexity and use only constant extra space.

LeetCode Problem: Link | Click Here

class Solution {

    public int singleNumber(int[] nums) {
        int numsLength = nums.length;

        // If there's only one element in the array, return that element
        if(numsLength==1){
            return nums[0];
        }
        else {
            int appears = -1; // Initialize a variable to store the single appearing number
            for (int i = 0; i < numsLength; i++) {
                int counts = 1; // Counter for the number of appearances of nums[i]

                // Loop to check if there are duplicate occurrences of nums[i] in the array
                for (int j=0; j<numsLength; j++){
                    if((nums[i]==nums[j]) && (i!=j)){ // If a duplicate is found
                        counts++; // Increment the count
                        break; // Exit the loop since duplicate found
                    }
                }

                // If the count for nums[i] is 1, it's the single appearing number, store it and break
                if(counts==1){
                    appears=nums[i];
                    break;
                }
            }
            return appears; // Return the single appearing number
        }
    }
}

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.