forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
47 lines (39 loc) · 1.59 KB
/
Solution.java
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
45
46
47
//Problem: https://www.hackerrank.com/challenges/sock-merchant
//Java 8
/*
Thoughts: Since we only care about the number of pairs, we can iterate over the
input and check if we have already seen a matching sock. To do this,
once we see a new sock we store it in a hash map with the value 1 and
if we see that sock a again we simply set its' value to 0 and increase
our pair count by 1. After iterating all the socks we will have a total
number of pairs.
Time Complexity: O(n) //We iterate over the input
Space Complexity: O(n) //At most every input is unique in our hashmap
*/
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
import java.util.HashMap;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
HashMap inventory = new HashMap<Integer,Integer>();
int matchingPairs = 0;
for(int i=0; i < n; i++)
{
int color = in.nextInt();
//This checks if we already have 1 sock of that color and if we do then we increment matchingPairs and set unmatched //socks of that color back to 0
if(inventory.containsKey(color) && inventory.get(color).equals(new Integer(1)))
{
inventory.put(color,0);
matchingPairs++;
continue;
}
inventory.put(color,1);
}
System.out.println(matchingPairs);
}
}