Home > database >  Sort JS array by number of unique entries in seperate array
Sort JS array by number of unique entries in seperate array

Time:12-19

So, I have an array of words. Lets say

const wordList = [    
    "aa",
    "aah",
    "aahed",
    "aahing",
    "aahs",
    "aal",
    "aalii",
    "aaliis",
    "aals",
    "aardvark",
    "aardvarks",
    "aardwolf",
    "aardwolves",
    "aargh",
    "aarrgh",
    "aarrghh",
    "aarti",
    "aartis",
    "aas",
    "aasvogel",
    "aasvogels",
]

Now, lets say I have a separate array of letters A-Y (excluding X) called letters

What I want to do is sort wordList by the amount of elements of letters it contains. Here, "arrdwolved" would be ranked considerably high because it has 9 unique letters (as in, letters are only counted once). Then, the letters in the first element of wordlist get removed from the array until there are elements in letters left, where it resets letters

Any ideas of how to do this? I am drawing an absolute blank here.

I imagine you could loop through every letter in each string, keeping a list of letters already "scored," and then sorting based on that, but I don't have a clue how I would implement this.

CodePudding user response:

You can use a Set to get the number of unique characters, then use Array.sort:

const wordList=["aa","aah","aahed","aahing","aahs","aal","aalii","aaliis","aals","aardvark","aardvarks","aardwolf","aardwolves","aargh","aarrgh","aarrghh","aarti","aartis","aas","aasvogel","aasvogels"];

const numOfUnique = (s) => new Set(s).size
const result = wordList.sort((a, b) => numOfUnique(b) - numOfUnique(a))
console.log(result)

CodePudding user response:

You could build an object with the scores and sort the array by this values descending.

const
    wordList = ["aa", "aah", "aahed", "aahing", "aahs", "aal", "aalii", "aaliis", "aals", "aardvark", "aardvarks", "aardwolf", "aardwolves", "aargh", "aarrgh", "aarrghh", "aarti", "aartis", "aas", "aasvogel", "aasvogels"],
    scores = Object.fromEntries(wordList.map(s => [
        s,
        s.split('').reduce((s, c) => s.add(c), new Set).size
    ]));

wordList.sort((a, b) => scores[b] - scores[a]);

console.log(wordList);
console.log(scores);
.as-console-wrapper { max-height: 100% !important; top: 0; }

CodePudding user response:

This should work, if i understood the question correctly

const wordList = [    
    "aa",
    "aah",
    "aahed",
    "aahing",
    "aahs",
    "aal",
    "aalii",
    "aaliis",
    "aals",
    "aardvark",
    "aardvarks",
    "aardwolf",
    "aardwolves",
    "aargh",
    "aarrgh",
    "aarrghh",
    "aarti",
    "aartis",
    "aas",
    "aasvogel",
    "aasvogels",
]

const letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z'];

// Sort the wordList array in descending order based on the number of unique elements from the letters array that each word contains
const sortedWordList = wordList.sort((a, b) => {
  const aSet = new Set(a.split(''));
  const bSet = new Set(b.split(''));
  return [...bSet].filter(x => letters.includes(x)).length - [...aSet].filter(x => letters.includes(x)).length;
});

console.log(sortedWordList);

// Remove the used letters from the letters array
for (const word of sortedWordList) {
  for (const letter of word) {
    const index = letters.indexOf(letter);
    if (index > -1) {
      letters.splice(index, 1);
    }
  }
  if (letters.length === 0) {
    letters = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'Y', 'Z'];
  }
}

console.log(letters);

First sort the wordList array in descending order based on the number of unique elements from the letters array that each word contains. We do this by creating a Set from each word and filtering it to only include elements that are present in the letters array, and then comparing the lengths of the filtered sets for each word.

Then, it iterates through the sorted wordList array and removes the used letters from the letters array. If the letters array becomes empty, it resets it to its original state.

At the end, the letters array will contain only the unused letters.

  • Related