-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem2.java
More file actions
35 lines (29 loc) · 1.16 KB
/
Problem2.java
File metadata and controls
35 lines (29 loc) · 1.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
/* Write the Java method below that takes an integer array freq and a character array text.
These two arrays have the same length. The method returns a character array that has freq[0] repetitions
of the first character in the text array, then freq[1] repetitions of the next character in the text array,
and so on. */
public class Problem2 {
public static void main(String[] args) {
int[] fre = {3, 6, 2, 5, 2, 1};
char[] letters = {'m', 's', 'k', 'p', 'y', 'q'};
char[] array = repeatChars(fre, letters);
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
public static char[] repeatChars(int[] freq, char[] text) {
int j = 0;
for (int i = 0; i < freq.length; i++) { //counting the length of our new array
j += freq[i];
}
System.out.println(j); // length
int index = 0;
char[] repeat = new char[j];
for (int i = 0; i < freq.length; i++) { //move around freq array
for (int r = 0; r < freq[i]; r++) {
repeat[index++] = text[i];
}
}
return repeat;
}
}