From 9c622c1aa4d53f6859b0b33adcb8b3cb2995e37c Mon Sep 17 00:00:00 2001 From: Sami Date: Fri, 8 Nov 2024 06:52:31 -0500 Subject: [PATCH] fix: added a new challenge --- PairedSocks/PairedSocks.java | 39 ++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 PairedSocks/PairedSocks.java diff --git a/PairedSocks/PairedSocks.java b/PairedSocks/PairedSocks.java new file mode 100644 index 0000000..6aca96b --- /dev/null +++ b/PairedSocks/PairedSocks.java @@ -0,0 +1,39 @@ +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Scanner; + +/** + * PairedSocks + */ +public class PairedSocks { + public static void main(String[] args) { + + Scanner sc = new Scanner(System.in); + int n = sc.nextInt(); + int[] randSocks = new int[n]; + for (int i = 0; i < randSocks.length; i++) { + randSocks[i] = sc.nextInt(); + } + sc.close(); + int pairCounted = pairingSocks(n, randSocks); + System.out.println(pairCounted); + } + + public static int pairingSocks(int n, int[] randSocks) { + int socks = 0; + Map pairCount = new HashMap<>(); + // we are gonna go through the array of integer and check if there is 2 paires + for (int x : randSocks) { + System.out.print(x + "\t"); + pairCount.put(x, pairCount.getOrDefault(x, 0) + 1); + } + // count how many paires there is and return the sum. + System.out.println("\n"); + for (int c : pairCount.values()) { + socks = socks + c / 2; + } + return socks; + } + +}