-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMethods.java
More file actions
106 lines (91 loc) · 3.44 KB
/
GenericMethods.java
File metadata and controls
106 lines (91 loc) · 3.44 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
public class GenericMethods {
// 1. Remove duplicates from an ArrayList
public static <E> ArrayList<E> removeDuplicates(ArrayList<E> list) {
// Using a HashSet to filter out duplicates since a Set does not allow duplicates.
HashSet<E> set = new HashSet<>(list);
return new ArrayList<>(set);
}
// 2. Linear search on an array
public static <E extends Comparable<E>> int linearSearch(E[] list, E key) {
for (int i = 0; i < list.length; i++) {
if (list[i].compareTo(key) == 0) {
return i;
}
}
return -1; // Return -1 if key is not found
}
// 3. Find the maximum element in an array
public static <E extends Comparable<E>> E max(E[] list) {
if (list == null || list.length == 0) {
return null; // Return null if the array is empty
}
E max = list[0];
for (E element : list) {
if (element.compareTo(max) > 0) {
max = element;
}
}
return max;
}
// 4. Binary search on a sorted array
public static <E extends Comparable<E>> int binarySearch(E[] list, E key) {
int low = 0;
int high = list.length - 1;
while (low <= high) {
int mid = (low + high) / 2;
if (list[mid].compareTo(key) < 0) {
low = mid + 1;
} else if (list[mid].compareTo(key) > 0) {
high = mid - 1;
} else {
return mid; // Return index of the found key
}
}
return -1; // Return -1 if key is not found
}
// 5. Sort an array
public static <E extends Comparable<E>> void sort(E[] list) {
Arrays.sort(list);
}
// 6. Find the maximum element in an ArrayList
public static <E extends Comparable<E>> E max(ArrayList<E> list) {
if (list == null || list.isEmpty()) {
return null; // Return null if the list is empty
}
E max = list.get(0);
for (E element : list) {
if (element.compareTo(max) > 0) {
max = element;
}
}
return max;
}
// Main method to test the above methods
public static void main(String[] args) {
// Example ArrayList
ArrayList<Integer> arrayList = new ArrayList<>(Arrays.asList(1, 2, 3, 3, 4, 4, 5));
System.out.println("Original List: " + arrayList);
System.out.println("List after removing duplicates: " + removeDuplicates(arrayList));
// Example array
Integer[] array = {2, 3, 1, 5, 4};
System.out.println("Original Array: " + Arrays.toString(array));
// Linear search
int index = linearSearch(array, 3);
System.out.println("Linear Search: Element 3 found at index: " + index);
// Find max in array
Integer maxElement = max(array);
System.out.println("Max element in array: " + maxElement);
// Sort array
sort(array);
System.out.println("Sorted Array: " + Arrays.toString(array));
// Binary search
int binarySearchIndex = binarySearch(array, 4);
System.out.println("Binary Search: Element 4 found at index: " + binarySearchIndex);
// Find max in ArrayList
Integer maxInList = max(arrayList);
System.out.println("Max element in ArrayList: " + maxInList);
}
}