Given a deck of cards, each card has an integer written on it.

At this point, you need to select a number X, so that we can divide the whole deck into 1 or more groups according to the following rules:

Each group has X cards. All the cards in the group have the same integer written on them. Return true only if your optional X >= 2. Example 1:

Input: [1,2,3,4,4,3,2,1] output:trueExplanation: possible groupings are [1,1], [2,2], [3,3], [4,4]Copy the code

Example 2:

Input: [1,1,1,2,2,2,3,3] output:falseExplanation: There is no grouping that meets the requirements.Copy the code

Example 3:

Input: [1,1] output:trueExplanation: possible groupings are [1,1]Copy the code

Example 4:

Input: [1,1,2,2,2,2] output:trueExplanation: possible groupings are [1,1], [2,2], [2,2]Copy the code

Source: LeetCode link: leetcode-cn.com/problems/x-…

Ideas:

  1. Sort the array first (ascending or descending)
  2. Loop through the number group, first take the first and second digit comparison, and so on, take the second and third digit comparison. And you end up with a two-dimensional array
  3. Determine if each array is a multiple of the minimum length
  const isSuccess = dist.every(i => i.length % min === 0)
Copy the code

The final code:

functionCardNumber (ary) {const resort = ary.sort((a, b) => a-b) // number.max_safe_INTEGERlet min = Number.MAX_SAFE_INTEGER
  const dist = []
  for (leti = 0; i < resort.length; I++) {// temporarily store an array of variables, recording the first elementletTem = [] tem.push(resort[I]) // Start from the secondfor (letj = i + 1; j < resort.length + 1;) {// If the next number is the same as the previous one, it is stored in a temporary groupif(resort mixes [j] = = = resort mixes [I]) {tem. Push (resort mixes [j]) / / let j++ go to perform the next step j++}else{// If the number of current groups is smaller than the number of temporary arraysif(min > tem.length) {min = tem.length} dist. Push (tem) tem = null J - 1, I = j - 1 j++ // it doesn't seem to be necessary here because it's already done herebreakbreak;
      }
    }
  }
  console.log(dist)
  const isSuccess = dist.every(i => i.length % min === 0)
  returnIsSuccess} const result2 = cardNumber([1,2,3,4,4,4, 3,2,1]) console.log(result2) //true
Copy the code

But:

  • If the minimum length is 1, then…

Finally, slightly forbidden code:

functionCardNumber (ary) {// If the length of the array is less than 2, the value is returnedfalse
  if (ary.length < 2) return falseConst resort = ary.sort((a, b) => a-b) // number.max_safe_INTEGER means the largest safe integer in JavaScriptlet min = Number.MAX_SAFE_INTEGER
  const dist = []
  for (leti = 0; i < resort.length; I++) {// temporarily store an array of variables, recording the first elementletTem = [] tem.push(resort[I]) // Start from the secondfor (letj = i + 1; j < resort.length + 1;) {// If the next array is the same as the previous one, it is stored in a temporary groupif (resort[j] === resort[i]) {
        tem.push(resort[j])
        j++
      } else{// add one here. If the temporary array is less than 2, there is no need to go downreturn false
        if (tem.length < 2) { 
          return false} // If the number of current groups is greater than the number of temporary arraysif(min > tem.length) {min = tem.length} dist. Push (tem) tem = null And since I ++ is going to be performed up here, it's going to be j-1, I = j-1break;
      }
    }
  }
  console.log(dist)
  const isSuccess = dist.every(i => i.length % min === 0)
  return isSuccess
}

Copy the code

Make a little progress every day