Skip to content

Commit 2e6cfcf

Browse files
Implement SortArray class for sorting integers
1 parent e573f77 commit 2e6cfcf

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

Arrays/SortArray.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
class SortArray {
2+
public int[] sortArray(int[] nums) {
3+
Map<Integer, Integer> map = new TreeMap<>();
4+
5+
for(int i = 0; i<nums.length; i++){
6+
map.put(nums[i], map.getOrDefault(nums[i], 0)+1);
7+
}
8+
9+
int pos = 0;
10+
for(int key : map.keySet()){
11+
int val = map.get(key);
12+
for(int i = 0; i<val; i++){
13+
nums[i+pos] = key;
14+
}
15+
pos = pos+val;
16+
}
17+
18+
return nums;
19+
}
20+
}

0 commit comments

Comments
 (0)