nikamirulmukmeen
BAN USER- 1of 1 vote
AnswersGiven a sorted array of integers, using the same array, shuffle the integers to have unique elements and return the index.
- nikamirulmukmeen in United States
Sample input : [3, 3, 4, 5, 5, 6, 7, 7, 7]
Sample output : [3, 4, 5, 6, 7, X, X, X, X]
In this case, it returns an index of 4.
The elements in the array after that index is negligible (don't care what value it is).| Report Duplicate | Flag | PURGE
Bloomberg LP Software Developer Problem Solving
public class Main {
public static void main(String[] args) {
int[] array = {3, 3, 4, 5, 5, 6, 7, 7, 7};
System.out.println(shuffle(array));
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
}
public static int shuffle(int[] array) {
int i = 0;
if (array.length == 1 || array.length == 0) {
return i;
}
for (int j = 1; j < array.length; j++) {
if (array[i] == array[j]) {
continue;
} else {
i++;
array[i] = array[j];
}
}
return i;
}
}
- nikamirulmukmeen March 19, 2015