Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add java solution for problem 136 & update readme
  • Loading branch information
anasmak04 committed Jan 25, 2023
commit c965a16f8773fad83e5be3675f0c282636176d65
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ LeetCode
|139|[Word Break](https://leetcode.com/problems/word-break/)| [C++](./algorithms/cpp/wordBreak/wordBreak.cpp)|Medium|
|138|[Copy List with Random Pointer](https://leetcode.com/problems/copy-list-with-random-pointer/)| [C++](./algorithms/cpp/copyListWithRandomPointer/copyListWithRandomPointer.cpp), [Python](./algorithms/python/CopyListWithRandomPointer/copyRandomList.py)|Hard|
|137|[Single Number II](https://leetcode.com/problems/single-number-ii/)| [C++](./algorithms/cpp/singleNumber/singleNumber.II.cpp), [Python](./algorithms/python/SingleNumberII/SingleNumberII.py)|Medium|
|136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp)|Medium|
|136|[Single Number](https://leetcode.com/problems/single-number/)| [C++](./algorithms/cpp/singleNumber/singleNumber.cpp), [Java](./algorithms/java/src/SingleNumber/SingleNumber.java)|Medium|
|135|[Candy](https://leetcode.com/problems/candy/)| [C++](./algorithms/cpp/candy/candy.cpp)|Hard|
|134|[Gas Station](https://leetcode.com/problems/gas-station/)| [C++](./algorithms/cpp/gasStation/gasStation.cpp)|Medium|
|133|[Clone Graph](https://leetcode.com/problems/clone-graph/)| [C++](./algorithms/cpp/cloneGraph/cloneGraph.cpp)|Medium|
Expand Down
50 changes: 50 additions & 0 deletions algorithms/java/src/SingleNumber/SingleNumber.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Source : https://leetcode.com/problems/single-number/description/
// Author : Anas Mak
// Date : 2023-01-25

/**********************************************************************************
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.

Here are few examples.
Example 1:

Input: nums = [2,2,1]
Output: 1
Example 2:

Input: nums = [4,1,2,1,2]
Output: 4
Example 3:

Input: nums = [1]
Output: 1

*
**********************************************************************************/
package Solution;

class Solution {
public int singleNumber(int[] nums) {

Set <Integer> single = new HashSet <> ();
for(int i : nums){
if(!single.contains(i)){
single.add(i);
}

else{
single.remove(i);
}
}

for(int result : single){
return result;
}

return -1;
}
}
21 changes: 21 additions & 0 deletions algorithms/java/src/SingleNumber/SingleNumberTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package Solution;

import org.junit.Assert;
import org.junit.Test;

/**
* Test for 58. Length of Last Word
*/
public class TestLengthOfLastWord {
@Test
public void test() {
Solution solution = new Solution();
int[] array1 = [2,2,1];
Assert.assertTrue(solution.SingleNumber(array1) == 1);
int[] array2 = [4,1,2,1,2];
Assert.assertTrue(solution.SingleNumber(array2) == 4);
int[] array3 = [1];
Assert.assertTrue(solution.SingleNumber(array3) == 1);

}
}