-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMaximumRepeatingNumber.java
91 lines (81 loc) · 2.49 KB
/
MaximumRepeatingNumber.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package Algorithms.IntegerArray;
import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;
/**
* Frequent element in an array or maximum repeating number in an array
*
* @author Srinivas Vadige, srinivas.vadige@gmail.com
* @since 13 Oct 2024
*/
class MaximumRepeatingNumber{
public static void main(String args[]){
int[] arr = new int[]{3,1,2,3,2,3,3,4,5};
System.out.println("maxRepeatingBruteForce: " + maxRepeatingBruteForce(arr) );
System.out.println("maxRepeatingUsingSort: " + maxRepeatingUsingSort(arr) );
System.out.println("maxRepeatingUsingHashMap: " + maxRepeatingUsingHashMap(arr) );
}
/**
* Brute Force
* @TimeComplexity O(n^2)
* @SpaceComplexity O(1)
*/
public static int maxRepeatingBruteForce(int[] nums) {
int maxRep = 0;
int maxCount = 0;
for (int i = 0; i < nums.length; i++) {
int tempCount = 0;
for (int j = 0; j < nums.length; j++) {
if (nums[i] == nums[j]) {
tempCount++;
}
}
if (maxCount < tempCount) {
maxCount = tempCount;
maxRep = nums[i];
}
}
return maxRep;
}
/**
* Using Sorting
* @TimeComplexity O(nlogn)
* @SpaceComplexity O(1)
*/
public static int maxRepeatingUsingSort(int[] nums) {
Arrays.sort(nums);
int maxRep = nums[0];
int maxCount = 1;
int tempCount = 1;
for (int i = 1; i < nums.length - 1; i++) {
if (nums[i-1] == nums[i]) {
tempCount++;
}
if(maxCount < tempCount) {
maxCount = tempCount;
maxRep = nums[i];
tempCount = 1;
}
}
return maxRep;
}
/**
* Using HashMap
* @TimeComplexity O(n)
* @SpaceComplexity O(n)
*/
@SuppressWarnings("unused")
public static int maxRepeatingUsingHashMap(int[] nums) {
int maxRep = 0;
int maxCount = 0;
Map<Integer, Integer> map = Arrays.stream(nums).boxed().collect(Collectors.groupingBy(Function.identity(), Collectors.summingInt(e->1)));
for(Map.Entry<Integer, Integer> e: map.entrySet()) {
if(maxCount < e.getValue()) {
maxCount = e.getValue();
maxRep = e.getKey();
}
}
return maxRep;
}
}