fix: added a new challenge
This commit is contained in:
@@ -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<Integer, Integer> 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user