given an array of integers, create a 2-dimensional array where the first element is distinct value from the array and the second element is that value's frequency within the array.Sort the ressulting array descending by frequency.If multiple values have the same frequency,they should be sorted ascending.
Answers
Answer:
see
Explanation:
Example: arr = [3,3,1,2,1] There are two values, 3&1 each within frequency of 2, and one value 2 with a frequency of 1: [[3,2], [1,2], [2,1]] Sort the 2-dimensional descending by frequency: [[3,2], [1,2], [2,1]] Sort the 2-dimensional array descending by values with matching frequencies [[1,2], [3,2], [2,1]
Answer:
By recording the frequency and returning separate arrays containing the number and its frequency in a parent array sorted in decreasing order, the question above suggests that it is a count and sort problem. There is one more need, though: The values should be arranged in accordance with their frequency if more than one value has it.
Explanation:
Complexity - O(n)
def groupSort(arr):
store = {}
for i in range(len(arr)):
if arr[i] not in store:
store[arr[i]] = 0
store[arr[i]] += 1
return sorted(list(store.items()), key=lambda x: (-1 * x[1], x[0]))
To know more about the sorting algorithm, click on the link below:
https://brainly.in/question/9966294
To know more about the complexity, click on the link below:
https://brainly.in/question/924407
#SPJ3