Paired colors

This commit is contained in:
Sami 2024-10-15 12:36:50 -04:00
parent 7ecd444efb
commit 0346f8e0be
2 changed files with 30 additions and 0 deletions

Binary file not shown.

View File

@ -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<Integer, Integer> 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;
}
}