技术求助:查找数组中反向动物元素的索引位置
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:
- 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.
- 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:
- 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.
- Count Occurrences: The key with a count of 1 corresponds to our abnormal element.
- 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

