You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

技术求助:查找数组中反向动物元素的索引位置

Find the Index of the Reversed Animal in the Array

Hey there! Let's tackle this problem where you need to locate the index of the reversed animal name in an array full of identical correct spellings. First, let's break down why your array.sort() approach wasn't working.

Why sort() Fails

When you sort the array, you're rearranging elements from their original positions. The reversed string's position in the sorted array has no connection to its index in the original input. For example, if your input is ['sheep', 'sheep', 'peehs', 'sheep'], sorting gives ['peehs', 'sheep', 'sheep', 'sheep']—but the reversed element was originally at index 2, not 0. Sorting erases the positional information we need!

Solution 1: Identify the Correct Animal First

Since there's only one reversed element, the first three elements will definitely include at least two instances of the correct animal name. We can use this to determine the valid spelling, then scan the array for the element that reverses to match it.

function findWrongWayAnimal(field) {
  // Determine the correct animal using the first three elements
  let correctAnimal;
  if (field[0] === field[1] || field[0] === field[2]) {
    correctAnimal = field[0];
  } else {
    // If field[0] doesn't match 1 or 2, field[1] must be the correct one
    correctAnimal = field[1];
  }

  // Iterate to find the reversed element
  for (let i = 0; i < field.length; i++) {
    const currentAnimal = field[i];
    const reversedCurrent = currentAnimal.split('').reverse().join('');
    
    if (currentAnimal !== correctAnimal && reversedCurrent === correctAnimal) {
      return i;
    }
  }
}

How This Works:

  1. Pinpoint the Correct Spelling: We check the first three elements—since only one entry is reversed, at least two of these will be the valid animal name.
  2. Scan for the Anomaly: For each element, we reverse it and compare to the correct spelling. The first element that matches this condition is our target, so we return its index immediately.

Solution 2: Robust Occurrence Counting

If you want an approach that handles edge cases (like shorter arrays, though the problem implies longer inputs), you can count occurrences of "normalized" animal names (treating the correct spelling and its reverse as a single group).

function findWrongWayAnimal(field) {
  const occurrenceCount = {};

  // Normalize each animal to its alphabetically smaller form (to group reverses)
  field.forEach(animal => {
    const reversedAnimal = animal.split('').reverse().join('');
    const key = animal < reversedAnimal ? animal : reversedAnimal;
    occurrenceCount[key] = (occurrenceCount[key] || 0) + 1;
  });

  // Find the key with only one occurrence (this is our abnormal pair)
  const abnormalKey = Object.keys(occurrenceCount).find(key => occurrenceCount[key] === 1);

  // Locate the index of the abnormal element in the original array
  return field.findIndex(animal => {
    const reversedAnimal = animal.split('').reverse().join('');
    return (animal === abnormalKey || reversedAnimal === abnormalKey) && 
           occurrenceCount[animal < reversedAnimal ? animal : reversedAnimal] === 1;
  });
}

How This Works:

  1. Normalize Keys: We convert each animal name to its alphabetically smaller version (e.g., 'peehs' becomes 'sheep' since 'sheep' is lex smaller). This groups correct and reversed spellings under the same key.
  2. Count Occurrences: The key with a count of 1 corresponds to our abnormal element.
  3. Find the Original Index: We scan the input array to locate which element belongs to that low-count key, then return its index.

Both solutions reliably find the reversed animal's index without losing track of the original array's structure.

内容的提问来源于stack exchange,提问作者VisualXZ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:25:58