-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayUtils.java
More file actions
182 lines (163 loc) · 5.19 KB
/
ArrayUtils.java
File metadata and controls
182 lines (163 loc) · 5.19 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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/**
* ArrayUtils - Utility class for array operations
* Includes: Searching, reversing, finding max/min, summing, averaging, removing duplicates, checking containment, and rotating arrays
*/
public class ArrayUtils {
/**
* Searches for a value in a sorted array using binary search
* @param array the sorted array to search
* @param target the value to find
* @return the index of the target, or -1 if not found
*/
public static int binarySearch(int[] array, int target) {
int left = 0;
int right = array.length - 1;
while (left <= right) {
int mid = left + (right - left) / 2;
if (array[mid] == target) {
return mid;
} else if (array[mid] < target) {
left = mid + 1;
} else {
right = mid - 1;
}
}
return -1;
}
/**
* Reverses an array in place
* @param array the array to reverse
*/
public static void reverse(int[] array) {
int left = 0;
int right = array.length - 1;
while (left < right) {
int temp = array[left];
array[left] = array[right];
array[right] = temp;
left++;
right--;
}
}
/**
* Finds the maximum value in an array
* @param array the array to search
* @return the maximum value
* @throws IllegalArgumentException if array is empty
*/
public static int findMax(int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty");
}
int max = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] > max) {
max = array[i];
}
}
return max;
}
/**
* Finds the minimum value in an array
* @param array the array to search
* @return the minimum value
* @throws IllegalArgumentException if array is empty
*/
public static int findMin(int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty");
}
int min = array[0];
for (int i = 1; i < array.length; i++) {
if (array[i] < min) {
min = array[i];
}
}
return min;
}
/**
* Calculates the sum of all elements in an array
* @param array the array to sum
* @return the sum of all elements
*/
public static int sum(int[] array) {
int total = 0;
for (int value : array) {
total += value;
}
return total;
}
/**
* Calculates the average of all elements in an array
* @param array the array to average
* @return the average as a double
* @throws IllegalArgumentException if array is empty
*/
public static double average(int[] array) {
if (array.length == 0) {
throw new IllegalArgumentException("Array cannot be empty");
}
return (double) sum(array) / array.length;
}
/**
* Removes duplicates from an array
* @param array the array to remove duplicates from
* @return a new array with duplicates removed
*/
public static int[] removeDuplicates(int[] array) {
if (array.length == 0) {
return array;
}
java.util.Set<Integer> set = new java.util.HashSet<>();
for (int value : array) {
set.add(value);
}
int[] result = new int[set.size()];
int index = 0;
for (int value : set) {
result[index++] = value;
}
return result;
}
/**
* Checks if an array contains a specific value
* @param array the array to search
* @param target the value to find
* @return true if the array contains the target, false otherwise
*/
public static boolean contains(int[] array, int target) {
for (int value : array) {
if (value == target) {
return true;
}
}
return false;
}
/**
* Rotates an array to the right by a given amount
* @param array the array to rotate
* @param steps the number of positions to rotate right
*/
public static void rotateRight(int[] array, int steps) {
if (array.length == 0) return;
steps = steps % array.length;
reverse(array, 0, array.length - 1);
reverse(array, 0, steps - 1);
reverse(array, steps, array.length - 1);
}
/**
* Helper method to reverse a portion of an array
* @param array the array to reverse
* @param start the starting index
* @param end the ending index (inclusive)
*/
private static void reverse(int[] array, int start, int end) {
while (start < end) {
int temp = array[start];
array[start] = array[end];
array[end] = temp;
start++;
end--;
}
}
}