-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathN3Array.java
More file actions
41 lines (39 loc) · 779 Bytes
/
N3Array.java
File metadata and controls
41 lines (39 loc) · 779 Bytes
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
import java.util.*;
import java.util.Map.Entry;
public class N3Array {
public static void n3array(int arr[])
{
int n=arr.length;
Map<Integer,Integer>hm=new HashMap<>();
int count=0;
for(int i=0;i<arr.length;i++)
{
if(!hm.containsKey(arr[i]))
{
hm.put(arr[i],1);
}
else
{
if(hm.get(arr[i])>=1)
{
count=hm.get(arr[i]);
hm.put(arr[i],count+1);
}
}
}
Iterator<Map.Entry<Integer,Integer>>it=hm.entrySet().iterator();
while(it.hasNext())
{
Entry<Integer, Integer> entry=it.next();
if(entry.getValue()>(n/3))
{
System.out.println(entry.getKey());
}
}
}
public static void main(String []args)
{
int arr[]= {3, 3, 4, 2, 4, 4, 2, 4, 4};
n3array(arr);
}
}