LeetCode/cs/rank-transform-of-an-array.cs

15 lines
394 B
C#
Raw Normal View History

public class Solution {
public int[] ArrayRankTransform(int[] arr) {
var unique = new SortedSet<int>();
foreach (var x in arr) {
unique.Add(x);
}
var ranks = unique.Select((item, index) => (item, index + 1)).ToDictionary();
for (int i = 0; i < arr.Length; ++i) {
arr[i] = ranks[arr[i]];
}
return arr;
}
}