-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuestion260.java
More file actions
44 lines (37 loc) · 828 Bytes
/
Copy pathQuestion260.java
File metadata and controls
44 lines (37 loc) · 828 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
Author: Ananthanarayanan R
Section: Algorithms
Question : 260
*/
import java.util.*;
public class Question260
{
public static int[] singleNumber(int[] nums)
{
if(nums.length==2)
return nums;
HashMap<Integer,Integer> map = new HashMap<>();
int[] result = new int[2];
Integer oldValue;
for(int num:nums)
{
if((oldValue = map.put(num,1))!=null)
map.put(num,oldValue+1);
}
int index = 0;
for(Map.Entry<Integer,Integer> entry:map.entrySet())
{
if(entry.getValue()==1)
result[index++] = entry.getKey();
if(index==2)
break;
}
return result;
}
public static void main(String[] args)
{
int[] nums = {1,1,2,3,4,5,6,7,8,9,9};
int[] result = singleNumber(nums);
System.out.println(result[0] + " and "+result[1]);
}
}