-
-
Notifications
You must be signed in to change notification settings - Fork 155
Added solution to leetcode problem 137 #1429
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
115 changes: 115 additions & 0 deletions
115
dsa-solutions/lc-solutions/0100-0199/0137-Single-Number-II
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
--- | ||
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 | ||
|
||
<Tabs> | ||
|
||
|
||
<TabItem value="Python" label="Python" default> | ||
|
||
<SolutionAuthor name="@ImmidiSivani"/> | ||
|
||
```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 | ||
``` | ||
</TabItem> | ||
<TabItem value="Java" label="Java"> | ||
<SolutionAuthor name="@ImmidiSivani"/> | ||
|
||
```Java | ||
class Solution { | ||
public int singleNumber(int[] nums) { | ||
Map<Integer,Integer> 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; | ||
|
||
} | ||
} | ||
``` | ||
</TabItem> | ||
</Tabs> | ||
|
||
#### 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/) | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.