Paired colors
This commit is contained in:
parent
7ecd444efb
commit
0346f8e0be
BIN
MatchColors/MatchPairedColors.class
Normal file
BIN
MatchColors/MatchPairedColors.class
Normal file
Binary file not shown.
30
MatchColors/MatchPairedColors.java
Normal file
30
MatchColors/MatchPairedColors.java
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user