From 187502c602b6e72183df5b03afa3c33aae334187 Mon Sep 17 00:00:00 2001 From: ImmidiSivani <147423543+ImmidiSivani@users.noreply.github.com> Date: Sun, 16 Jun 2024 19:07:54 +0530 Subject: [PATCH 1/2] added solution to leetcode problem 137 --- .../0100-0199/0137-Single-Number-II | 114 ++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II diff --git a/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II b/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II new file mode 100644 index 000000000..b8bc8ae49 --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II @@ -0,0 +1,114 @@ +--- +id: Single-Number-II +title: Single Number II +sidebar_label: 0136 Single Number II +tags: + - Java + - Python + - C++ + +description: "This is a solution to the Single Number ii problem on LeetCode." +--- + +## Problem Description + +Given an integer array nums where every element appears three times except for one, which appears exactly once. Find the single element and return it. + +You must implement a solution with a linear runtime complexity and use only constant extra space. + + + +### Examples + +**Example 1:** + +``` +Input: nums = [2,2,3,2] +Output: 3 + +``` + +**Example 2:** + +Input: nums = [0,1,0,1,0,1,99] +Output: 99 + + + +### Constraints + +- $1 \leq nums.length \leq 3 * 10^4$ +- $-3 \times 10^4 \leq nums[i] \leq 3 \times 10^4$ +- Each element in the array appears three times except for one element which appears only once. + +--- + +## Solution for Single Number ii Problem + +### Intuition + +The problem states that every element in the array appears exactly three times, except for one element that appears exactly once. To identify this unique element efficiently, we can leverage a hash map to count the occurrences of each element. The key observation here is that the unique element will have a count of one in the hash map, while all other elements will have a count of three. +### Approach + +First declare an empty hashmap then we store the each element and it's frequencies in hashmap after that we iterate to the keys of map if the value of particular key is 1 then we return the key. + +#### Code in Different Languages + + + + + ```java + class Solution { + public int singleNumber(int[] nums) { + Map mp=new HashMap<>(); + //int[] a=new int[2]; + int a=0; + for(int i:nums){ + mp.put(i,1+mp.getOrDefault(i,0)); + } + for(int i:mp.keySet()){ + if(mp.get(i)==1){ + a=i; + break; + } + } + return a; + + } +} + +````` + + + + + +````python +class Solution: + def singleNumber(self, nums: List[int]) -> int: + mp = {} + for num in nums: + if num in mp: + mp[num] += 1 + else: + mp[num] = 1 + + for num, c in mp.items(): + if c == 1: + return num + +````` + + + +#### Complexity Analysis + +- Time complexity: $O(n)$, as we're iterating over the array only once. +- Space complexity: $O(n)$, because here we are using Hashmap to count occurences of elements. + +## References + +- **LeetCode Problem:** [Single Number ii](https://leetcode.com/problems/single-number-ii/description/) +- **Solution Link:** [Single Number ii on LeetCode](https://leetcode.com/problems/single-number-ii/post-solution/?submissionId=1273177780) +- **Authors Leetcode Profile:** [SivaniImmidi](https://leetcode.com/u/SivaniImmidi/) +``` From 1a1ac5eb6c93edc260883e0246dbb1865c26404a Mon Sep 17 00:00:00 2001 From: ImmidiSivani <147423543+ImmidiSivani@users.noreply.github.com> Date: Mon, 17 Jun 2024 10:14:57 +0530 Subject: [PATCH 2/2] Update 0137-Single-Number-II --- .../0100-0199/0137-Single-Number-II | 55 ++++++++++--------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II b/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II index b8bc8ae49..378e5c72f 100644 --- a/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II +++ b/dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II @@ -55,9 +55,31 @@ First declare an empty hashmap then we store the each element and it's frequenci #### Code in Different Languages + + + + + + + ```Python + class Solution: + def singleNumber(self, nums: List[int]) -> int: + mp = {} + for num in nums: + if num in mp: + mp[num] += 1 + else: + mp[num] = 1 + + for num, c in mp.items(): + if c == 1: + return num +``` + - ```java + + ```Java class Solution { public int singleNumber(int[] nums) { Map mp=new HashMap<>(); @@ -74,32 +96,11 @@ First declare an empty hashmap then we store the each element and it's frequenci } return a; + } } -} - -````` - - - - - -````python -class Solution: - def singleNumber(self, nums: List[int]) -> int: - mp = {} - for num in nums: - if num in mp: - mp[num] += 1 - else: - mp[num] = 1 - - for num, c in mp.items(): - if c == 1: - return num - -````` - - +``` + + #### Complexity Analysis @@ -111,4 +112,4 @@ class Solution: - **LeetCode Problem:** [Single Number ii](https://leetcode.com/problems/single-number-ii/description/) - **Solution Link:** [Single Number ii on LeetCode](https://leetcode.com/problems/single-number-ii/post-solution/?submissionId=1273177780) - **Authors Leetcode Profile:** [SivaniImmidi](https://leetcode.com/u/SivaniImmidi/) -``` +