← Back to companies
[ OK ] Loaded —
[ INFO ]
$ cd
$ ls -lt
01
02
03
04
05
$ ls -lt
01
02
03
04
05
user@intervues:~/$
You are given an unsorted integer array of about 75 elements that may contain duplicates. Write a function that:
Input: [4, 3, 2, 3, 1]
Output: [1, 2, 4, 0]
Input: [1, 1, 1, 2, 2]
Output: [0, 0, 0, 0]
Input: [5, 3, 5, 5, 5, 4, 3, 4]
Output: [0, 0, 3, 4, 0, 0]
Here is a sample solution in Python:
`python def zero_out_duplicates_and_sort(arr): seen = set() for i in range(len(arr)): if arr[i] in seen or arr[i] == 0: arr[i] = 0 else: seen.add(arr[i]) arr.sort() return arr
arr = [4, 3, 2, 3, 1] print(zero_out_duplicates_and_sort(arr)) # Output: [1, 2, 4, 0] `
This solution first uses a set to track seen numbers and replaces duplicates with 0. Then, it sorts the array in ascending order.