Skip to content

Commit ab4470b

Browse files
authored
Merge pull request #1429 from ImmidiSivani/Leetcode-137
Added solution to leetcode problem 137
2 parents 23097b0 + 1a1ac5e commit ab4470b

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
id: Single-Number-II
3+
title: Single Number II
4+
sidebar_label: 0136 Single Number II
5+
tags:
6+
- Java
7+
- Python
8+
- C++
9+
10+
description: "This is a solution to the Single Number ii problem on LeetCode."
11+
---
12+
13+
## Problem Description
14+
15+
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.
16+
17+
You must implement a solution with a linear runtime complexity and use only constant extra space.
18+
19+
20+
21+
### Examples
22+
23+
**Example 1:**
24+
25+
```
26+
Input: nums = [2,2,3,2]
27+
Output: 3
28+
29+
```
30+
31+
**Example 2:**
32+
33+
Input: nums = [0,1,0,1,0,1,99]
34+
Output: 99
35+
36+
37+
38+
### Constraints
39+
40+
- $1 \leq nums.length \leq 3 * 10^4$
41+
- $-3 \times 10^4 \leq nums[i] \leq 3 \times 10^4$
42+
- Each element in the array appears three times except for one element which appears only once.
43+
44+
---
45+
46+
## Solution for Single Number ii Problem
47+
48+
### Intuition
49+
50+
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.
51+
### Approach
52+
53+
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.
54+
55+
#### Code in Different Languages
56+
57+
<Tabs>
58+
59+
60+
<TabItem value="Python" label="Python" default>
61+
62+
<SolutionAuthor name="@ImmidiSivani"/>
63+
64+
```Python
65+
class Solution:
66+
def singleNumber(self, nums: List[int]) -> int:
67+
mp = {}
68+
for num in nums:
69+
if num in mp:
70+
mp[num] += 1
71+
else:
72+
mp[num] = 1
73+
74+
for num, c in mp.items():
75+
if c == 1:
76+
return num
77+
```
78+
</TabItem>
79+
<TabItem value="Java" label="Java">
80+
<SolutionAuthor name="@ImmidiSivani"/>
81+
82+
```Java
83+
class Solution {
84+
public int singleNumber(int[] nums) {
85+
Map<Integer,Integer> mp=new HashMap<>();
86+
//int[] a=new int[2];
87+
int a=0;
88+
for(int i:nums){
89+
mp.put(i,1+mp.getOrDefault(i,0));
90+
}
91+
for(int i:mp.keySet()){
92+
if(mp.get(i)==1){
93+
a=i;
94+
break;
95+
}
96+
}
97+
return a;
98+
99+
}
100+
}
101+
```
102+
</TabItem>
103+
</Tabs>
104+
105+
#### Complexity Analysis
106+
107+
- Time complexity: $O(n)$, as we're iterating over the array only once.
108+
- Space complexity: $O(n)$, because here we are using Hashmap to count occurences of elements.
109+
110+
## References
111+
112+
- **LeetCode Problem:** [Single Number ii](https://leetcode.com/problems/single-number-ii/description/)
113+
- **Solution Link:** [Single Number ii on LeetCode](https://leetcode.com/problems/single-number-ii/post-solution/?submissionId=1273177780)
114+
- **Authors Leetcode Profile:** [SivaniImmidi](https://leetcode.com/u/SivaniImmidi/)
115+

0 commit comments

Comments
 (0)