diff --git a/MatchColors/MatchPairedColors.class b/MatchColors/MatchPairedColors.class new file mode 100644 index 0000000..a2e91a8 Binary files /dev/null and b/MatchColors/MatchPairedColors.class differ diff --git a/MatchColors/MatchPairedColors.java b/MatchColors/MatchPairedColors.java new file mode 100644 index 0000000..69fef6b --- /dev/null +++ b/MatchColors/MatchPairedColors.java @@ -0,0 +1,30 @@ +import java.util.HashMap; + +/** + * MatchPairedColors + */ +public class MatchPairedColors { + + public static void main(String[] args) { + + int[] ar = { 1, 1, 1, 1, 3, 2, 2, 3, 4 }; + System.out.println(numberOfPairedSocks(ar)); + } + + private static int numberOfPairedSocks(int[] arr) { + int numofP = 0; + HashMap socksCount = new HashMap<>(); + for (int sock : arr) { + if (socksCount.containsKey(sock)) { + socksCount.put(sock, socksCount.get(sock) + 1); + } else { + socksCount.put(sock, 1); + } + } + + for (int count : socksCount.values()) { + numofP += count / 2; + } + return numofP; + } +}