forked from shivaylamba/Hacktoberfest
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArrayElementFrequency.java
50 lines (40 loc) · 1.15 KB
/
ArrayElementFrequency.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;
public class ArrayElementFrequency {
// Complete the sockMerchant function below.
static int sockMerchant(int n, int[] ar) {
Arrays.sort(ar);
int sum=0, cnt=0, as=0;
for(int i=0;i<n;i+=cnt){
int x=ar[i];
cnt=0;
for(int j=i;j<n;j++){
if(x==ar[j])
cnt++;
}
sum+=(cnt/2);
if(cnt==0)
cnt++;
System.out.println(x+" : Occurs "+cnt+" times!");
}
return sum;
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
int[] ar = new int[n];
String[] arItems = br.readLine().split(" ");
for (int i = 0; i < n; i++) {
int arItem = Integer.parseInt(arItems[i]);
ar[i] = arItem;
}
int result = sockMerchant(n, ar);
System.out.println("Total Pairs: "+result);
br.close();
}
}