Pergunta de entrevista da empresa Sonos

In a language of your choice, please provide a solution for the following problem. Problem: Given a sorted array of non-zero integers, remove the duplicate elements from the array and return the number of unique elements found. At the end of your algorithm, the array should contain unique elements. If the number of unique elements is less than the size of the array, fill the remaining indices with 0. For example: ex. Input - arr[] = {1, 2, 2, 3} ex. Output - arr[] = {1, 2, 3, 0} - return 3 ex. Input - arr[] = {1, 1, 1, 1} ex. Output - arr[] = {1, 0, 0, 0} - return 1 You should include your algorithm in the removec Duplicates method, whose inputs are the array and the length of the array.

Resposta da entrevista

Sigiloso

21 de mar. de 2022

for (int i = 0; i < arr.length; i++) { System.out.print(arr[i] + " "); } System.out.println("\nFound " + uniqueElementsFound + " unique elements"); } // place logic here private static int removeDuplicates(int arr[], int n) { int dublicates =0; int count =0; int temp =0; for(int i=0; i

3