Check if Array is Good

EasyArrayHash TableSorting

Solution

export function isGood(nums: number[]): boolean {
  const n = nums.length - 1;
  const counter = new Map<number, number>();
 
  for (const num of nums) {
    if (n < num) {
      return false;
    }
    counter.set(num, (counter.get(num) ?? 0) + 1);
  }
 
  return [...counter].every(([num, count]) => (num === n ? count === 2 : count === 1));
}