简体   繁体   English

在数组(JS)中查找第一个唯一的非七字串

[英]Find the first Unique Non-anagram strings in array (JS)

I'm trying to find the first unique string in array ignoring all other anagrams for example : 我试图在忽略所有其他字谜的情况下找到数组中的第一个唯一字符串,例如:

let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares']; 让单词= ['小睡','老师','骗子','锅','耳朵','时代','公顷'];

output = nap teachers ear 输出=小睡老师的耳朵

or let words = ['rat', 'tar', 'art']; 或让单词= ['rat','tar','art'];

output rat 输出老鼠

I stuck here 我困在这里

let set = new Set();
let words = let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares']


let map = new Map();
for (let word of words) {
    let sorted = word.toLowerCase().split('').sort().join(''); 
    map.set(sorted, word);
    set.add(sorted)
}
let arr = Array.from(set)


for (let inx = 0; inx < words.length; inx++) {
    arr.push('0')
}
console.log(arr);
console.log(words);

function isAnagram(s1, s2){
    return s1.split("").sort().join("") === s2.split("").sort().join("");
  }

let result = [];

  for (let i = 0; i < words.length; i++) {
       if ()
       result.push(words[i])

}
console.log(result);

Instead of a Map of string: string in which you only store the last anagram option, you can create a map of string: [ string ] in which you store all options. 代替的Mapstring: string在其中只存储最后字谜选项,您可以创建地图的string: [ string ]您在其中存储的所有选项。

Then, in the end, you can loop over all those entries and return the first element of each array: 然后,最后,您可以遍历所有这些条目并返回每个数组的第一个元素:

 let set = new Set(); let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares'] let map = new Map(); for (let word of words) { let sorted = word.toLowerCase().split('').sort().join(''); if (!map.has(sorted)) map.set(sorted, []); set.add(sorted); map.get(sorted).push(word); } let arr = Array .from(set) .map(k => map.get(k)[0]) console.log(arr); 

You need to check the current position of the array "words" and then check the others one, this means you need to have a loop inside a loop, so your code should look like this: 您需要检查数组“ words”的当前位置,然后检查其他位置,这意味着您需要在一个循环内有一个循环,因此您的代码应如下所示:

 let set = new Set(); let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares'] let map = new Map(); for (let word of words) { let sorted = word.toLowerCase().split('').sort().join(''); map.set(sorted, word); set.add(sorted) } let arr = Array.from(set) for (let inx = 0; inx < words.length; inx++) { arr.push('0') } console.log(arr); console.log(words); function isAnagram(s1, s2){ console.log("checking: " + s1 + " and " + s2); return s1.split("").sort().join("") === s2.split("").sort().join(""); } let result = []; let anagram = false; for (let i = 0; i < words.length; i++) { for(let j=i+1; j<words.length; j++){ if (isAnagram(words[i], words[j])){ anagram = true; break; console.log("Is anagram!"); } } if(anagram) { //Before insert we will see if we have an anagram in the result array anagram = false; for(let z=0; z<result.length; z++) { //We will se if it exists in result if(isAnagram(words[i],result[z])) { anagram = true; break; } } if(!anagram) { result.push(words[i]); anagram = false; } } } console.log(result); 

Thanks to user3297291 I found even simpler variant 多亏了user3297291,我发现了更简单的变体

let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares']
const set = new Set();
const unique = [];

words.forEach((word) => {

 const sorted = word.split('').sort().join('');

 if (set.has(sorted)) {
   return;
 }
   unique.push(word);
   set.add(sorted);
 });

 print(unique.join(' '));

Another implementation. 另一个实现。 :) :)

function aClean(arr) {
     var map = new Map();
     arr.map(item => {
         var key = item.toLowerCase().split("").sort().join("");
         map.has(key) ? null : map.set(key, item)
     });
    return Array.from(map.values());
}

This should do the job: 这应该做的工作:

let words = ['nap', 'teachers', 'cheaters', 'pan', 'ear', 'era', 'hectares']

let map = new Map(
    words.reverse().map(word => {
        let sorted = word.toLowerCase().split('').sort().join(''); 
        return [sorted, word];
    })
);

console.log(Array.from(map.values())); // output: [ "teachers", "ear", "nap" ]

Using the characters from each word as a key. 使用每个单词的字符作为键。 Exemple: for [teacher, cheaters, hectares] the key is "aceehrst" 例如:对于[老师,作弊者,公顷],关键是“ aceehrst”

and since the order is important for finding the first one, reversing the order of words will help to keep "teacher" as a value for the key "aceehrst" instead of "hectares". 并且由于顺序对于找到第一个顺序很重要,因此颠倒单词顺序将有助于保持“老师”作为键“ aceehrst”而不是“公顷”的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM