-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProblem-3.java
71 lines (46 loc) · 1.87 KB
/
Problem-3.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/**
* @author AkashGoyal
* @date 31/05/2021
*/
/**
--------------------- Problem----------->> Given an array of size n and a number k, find all elements that appear more than " n/k " times.
Problem Link :- https://practice.geeksforgeeks.org/problems/count-element-occurences/1#
*/
//Method-1
public int countOccurence(int[] arr, int n, int k)
{
// your code here,return the answer
HashMap<Integer, Integer> hmap=new HashMap<>();
int global_count=0;
for(int i=0;i<n;i++)
{
if(!hmap.containsKey(arr[i]))
{
hmap.put(arr[i],1);
}
else
{
int count=hmap.get(arr[i]);
hmap.put(arr[i],count+1);
}
}
for(Map.Entry<Integer,Integer> entry: hmap.entrySet())
{
if(entry.getValue()>n/k)
{
global_count++;
}
}
return global_count;
}
//------------------------------------------------------------------------------------------------------------------------------------------------------------
//Method-2
/**
Steps:- (Moore's Algo):- (Can we solve it..????)
1. (n/2) occurences:-
https://www.youtube.com/watch?v=AoX3BPWNnoE&list=PLgUwDviBIf0rPG3Ictpu74YWBQ1CaBkm2&t=0s
2. (n/3) occurences:-
https://www.youtube.com/watch?v=yDbkQd9t2ig
3. (n/k) occurences:-
https://www.geeksforgeeks.org/given-an-array-of-of-size-n-finds-all-the-elements-that-appear-more-than-nk-times/
*/