forked from PacktPublishing/Java-Coding-Problems
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArraySearch.java
More file actions
161 lines (116 loc) · 4.16 KB
/
ArraySearch.java
File metadata and controls
161 lines (116 loc) · 4.16 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
package modern.challenge;
import java.util.Arrays;
import java.util.Comparator;
import java.util.stream.IntStream;
public final class ArraySearch {
private ArraySearch() {
throw new AssertionError("Cannot be instantiated");
}
public static boolean containsElementV1(int[] arr, int toContain) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
for (int elem : arr) {
if (elem == toContain) {
return true;
}
}
return false;
}
public static boolean containsElementV2(int[] arr, int toContain) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
Arrays.sort(arr);
int index = Arrays.binarySearch(arr, toContain);
return (index >= 0);
}
public static boolean containsElementV3(int[] arr, int toContain) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
return Arrays.stream(arr)
.anyMatch(e -> e == toContain);
}
public static <T> boolean containsElementObjectV1(T[] arr, T toContain) {
if (arr == null || toContain == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
for (T elem : arr) {
if (elem.equals(toContain)) {
return true;
}
}
return false;
}
public static <T> boolean containsElementObjectV2(T[] arr, T toContain, Comparator<? super T> c) {
if (arr == null || toContain == null || c == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
for (T elem : arr) {
if (c.compare(elem, toContain) == 0) {
return true;
}
}
return false;
}
public static <T> boolean containsElementObjectV3(T[] arr, T toContain, Comparator<? super T> c) {
if (arr == null || toContain == null || c == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
Arrays.sort(arr, c);
int index = Arrays.binarySearch(arr, toContain, c);
return (index >= 0);
}
public static int findIndexOfElementV1(int[] arr, int toFind) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i] == toFind) {
return i;
}
}
return -1;
}
public static int findIndexOfElementV2(int[] arr, int toFind) {
if (arr == null) {
throw new IllegalArgumentException("Array cannot be null");
}
return IntStream.range(0, arr.length)
.filter(i -> toFind == arr[i])
.findFirst()
.orElse(-1);
}
public static <T> int findIndexOfElementObjectV1(T[] arr, T toFind) {
if (arr == null || toFind == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
for (int i = 0; i < arr.length; i++) {
if (arr[i].equals(toFind)) {
return i;
}
}
return -1;
}
public static <T> int findIndexOfElementObjectV2(T[] arr, T toFind, Comparator<? super T> c) {
if (arr == null || toFind == null || c == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
for (int i = 0; i < arr.length; i++) {
if (c.compare(arr[i], toFind) == 0) {
return i;
}
}
return -1;
}
public static <T> int findIndexOfElementObjectV3(T[] arr, T toFind, Comparator<? super T> c) {
if (arr == null || toFind == null || c == null) {
throw new IllegalArgumentException("None of the arguments can be null");
}
return IntStream.range(0, arr.length)
.filter(i -> c.compare(toFind, arr[i]) == 0)
.findFirst()
.orElse(-1);
}
}