-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
77 lines (60 loc) · 2.05 KB
/
MainClass.java
File metadata and controls
77 lines (60 loc) · 2.05 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
package com.interview.all;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
public class MainClass {
//First Approach
public static void countOccurance() {
int visited = -1;
int[] arr = new int[] { 1, 2, 8, 3, 2, 2, 2, 5, 1 };
int frequency[] = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
int count = 1;
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] == arr[j]) {
count++;
frequency[j] = visited;
}
}
if(frequency[i]!=visited)
frequency[i] = count;
}
for (int i = 0; i < frequency.length; i++) {
if(frequency[i]!=visited)
System.out.println(arr[i]+" | "+ frequency[i]);
}
}
public static void main(String[] args) {
//Second Approach
List<Integer> list = Arrays.asList(1,3,5,8,3,4,11,6,2,6,89,21,11,34,44,56,78,99,23);
Map<Integer, Long> list_one = list.stream()
.collect(Collectors.groupingBy(Function.identity(),Collectors.counting()));
list_one.forEach((k,v)->{System.out.println(k+" "+v);});
// Third Approach write above in core java
Set<Integer> set = new HashSet<Integer>(list);
for(Integer ins :set) {
System.out.println(ins+" "+Collections.frequency(list,ins));
}
//find second max from list
int secondLargest = list.stream().sorted(Comparator.reverseOrder())
.limit(2).skip(1).findFirst().get();
System.out.println("secondLargest number is :"+secondLargest);
// can we declare abstract class as a final
//can we in declare interface data member as private
//how to search text on vim
// difference between Query param & Path Variable which one is optional
Map<Integer, Long> map = new HashMap<Integer, Long>();
System.out.println(0.1*3 == 0.3);
System.out.println(0.1*2 == 0.2);
//write a program for represent heap & stack
}
}