-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDegreeOfArray.java
More file actions
executable file
·77 lines (70 loc) · 2.17 KB
/
DegreeOfArray.java
File metadata and controls
executable file
·77 lines (70 loc) · 2.17 KB
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
import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Neel_Kapadia
*/
public class DegreeOfArray {
public int findShortestSubArray(int[] nums) {
LinkedHashMap<Integer, Integer> hm = new LinkedHashMap<>();
for (int i = 0; i < nums.length; i++) {
if (!hm.containsKey(nums[i])) {
hm.put(nums[i], 1);
} else {
int a = hm.get(nums[i]);
hm.put(nums[i], a + 1);
}
}
Iterator it = hm.entrySet().iterator();
int max = 0, key = 0;
while (it.hasNext()) {
Map.Entry p = (Map.Entry) it.next();
if (max < (int) p.getValue()) {
max = (int) p.getValue();
}
}
it = hm.entrySet().iterator();
int ans = Integer.MAX_VALUE;
while (it.hasNext()) {
Map.Entry p = (Map.Entry) it.next();
if ((int) p.getValue() == max) {
int a = findFirstIndex((int) p.getKey(), nums);
int b = findLastIndex((int) p.getKey(), nums);
if(ans > (b-a+1))
ans = (b-a+1);
System.out.println(b - a + 1);
}
}
return ans;
}
int findFirstIndex(int x, int[] nums) {
for (int i = 0; i < nums.length; i++) {
if (x == nums[i]) {
return i;
}
}
return -1;
}
int findLastIndex(int x, int[] nums) {
int loc = -1;
for (int i = 0; i < nums.length; i++) {
if (x == nums[i]) {
loc = i;
}
}
return loc;
}
public static void main(String[] args) {
DegreeOfArray d = new DegreeOfArray();
int nums[] = {1,2,2,3,1,4,2};
System.out.println(d.findShortestSubArray(nums));
}
}