-
Notifications
You must be signed in to change notification settings - Fork 0
/
DriverClass.java
65 lines (55 loc) · 1.77 KB
/
DriverClass.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
package aab180004;
import java.util.HashSet;
import java.util.Random;
/**
* @author Achyut Bhandiwad - aab180004
* @author Mythri Thippareddy - mxt172530
*/
public class DriverClass<T> {
static Random rand = new Random();
/**
* @param arr ; Return the number of unique elements
* @return
*/
static<T> int distinctElements(T[ ] arr) {
HashSet<T> hashMap2 = new HashSet<>();
for(int i=0;i<arr.length;i++) {
hashMap2.add(arr[i]);
}
return hashMap2.size();
}
/**
* @return : Creating an array of 1000000 elements
*/
public static Integer[] createArray() {
Integer[] arr = new Integer[1000000];
for(int i =0;i<arr.length;i++) {
arr[i]=rand.nextInt();
}
return arr;
}
/**
* @param args
*/
public static void main(String[] args) {
DriverClass<Integer> driver = new DriverClass<Integer>();
Timer timer = new Timer();
Integer[] arr =createArray();
timer.start();
int numberOfDistinctElementsUsingHashSet = DriverClass.distinctElements(arr);
timer.end();
System.out.println("Number of distinct elements using HashSet:"+numberOfDistinctElementsUsingHashSet);
System.out.println(timer);
timer.start();
int numberOfDistinctElementsUsingRobinHood = RobinHoodHashing.distinctElements(arr);
timer.end();
System.out.println("Number of distinct elements using RobinHoodHashing:"+numberOfDistinctElementsUsingRobinHood);
System.out.println(timer);
timer.start();
DoubleHashing<MyInteger,MyInteger> dh = new DoubleHashing<>();
int numberOfDistinctElementsUsingDoubleHashing = dh.distinctElements(arr);
timer.end();
System.out.println("Number of distinct elements using DoubleHashing:"+numberOfDistinctElementsUsingHashSet);
System.out.println(timer);
}
}