统计出现次数,判断是否重复
function uniqueOccurrences(arr: number[]): boolean {
let result: Array<number> = []
let length:number = 1
arr.sort((a, b) => a-b)
for(let i=1; i<arr.length; i++){
if(arr[i]===arr[i-1]){
length++
} else {
result.push(length)
length = 1
}
}
result.push(length)
result.sort((a, b) => a-b)
for(let j=1; j<result.length; j++){
if(result[j]===result[j-1]){
return false
}
}
return true
};
网友评论