Skip to content

Commit 6afbbc0

Browse files
author
m0a07xh
committed
Array related Util Methods
1 parent b2165f4 commit 6afbbc0

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

util/ArrayUtil.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,26 @@ public static int[] constructRandomUniqueElementsArray() {
4545
return set.stream().mapToInt(Integer::intValue).toArray();
4646
}
4747

48+
/**
49+
* Generate integer array of size range and each element between 1 to range
50+
*
51+
* @param range elements value will be between 1 to range
52+
* @return int array
53+
*/
54+
public static int[] constructArrayWithinRange(int range) {
55+
Set<Integer> set = new HashSet<Integer>();
56+
int[] arr = new int[range];
57+
int index = 0;
58+
while (index < range) {
59+
int data = AlgoUtils.random(range) + 1;
60+
if (!set.contains(data)) {
61+
arr[index++] = data;
62+
set.add(data);
63+
}
64+
}
65+
return arr;
66+
}
67+
4868
/**
4969
* Generate random integer array of size between 5 and 10
5070
* and value will be in range of 0 to 15

0 commit comments

Comments
 (0)