Home > Blockchain >  Compare an array with itself for duplicate and store in new array
Compare an array with itself for duplicate and store in new array

Time:08-15

I have a question, how do I compare values ​​in an array with themselves without the value to be compared being shown as true?

As an example, if the value EUW1_6011808396 occurs again, it should be stored in the array matchingMatches.

The Values of the Array are:

[EUW1_6011808396, EUW1_6011824351, EUW1_6011720277, EUW1_6010413995, EUW1_6010218048, EUW1_6010184913, EUW1_6010131700, EUW1_6009739853, EUW1_6008825456, EUW1_6008833322, EUW1_6008409245, EUW1_6008369887, EUW1_6008355242, EUW1_6007567238, EUW1_6007269146, EUW1_6007226284, EUW1_6007192332, EUW1_6005571988, EUW1_6005438941, EUW1_6005495312, EUW1_6013263286, EUW1_6013193252, EUW1_6012475324, EUW1_6012411610, EUW1_6012315128, EUW1_6012011561, EUW1_6011110477, EUW1_6011026046, EUW1_6009739853, EUW1_6006439870, EUW1_6006434580, EUW1_6005238786, EUW1_6005191249, EUW1_6005026992, EUW1_6005015187, EUW1_6004958241, EUW1_6003811368, EUW1_6002847479, EUW1_6002164371, EUW1_6002148723, EUW1_6015524685, EUW1_6015387328, EUW1_6015402003, EUW1_6014779337, EUW1_6014724668, EUW1_6014701498, EUW1_6014655368, EUW1_6014580839, EUW1_6014429620, EUW1_6014475971, EUW1_6014473252, EUW1_6013334881, EUW1_6013322375, EUW1_6012669749, EUW1_6012635347, EUW1_6012583396, EUW1_6010971941, EUW1_6006896961, EUW1_6006881165, EUW1_6006518887, EUW1_6015745842, EUW1_6015589872, EUW1_6014068520, EUW1_6014044304, EUW1_6007955310, EUW1_6003705297, EUW1_6003569783, EUW1_6002003834, EUW1_6000787500, EUW1_5994465297, EUW1_5993391050, EUW1_5992233473, EUW1_5992169601, EUW1_5984062877, EUW1_5984034743, EUW1_5983855739, EUW1_5983880569, EUW1_5983766086, EUW1_5982931745, EUW1_5982372929, EUW1_6005238786, EUW1_6005191249, EUW1_6005026992, EUW1_6005015187, EUW1_6004958241, EUW1_6002164371, EUW1_6002148723, EUW1_6002057259, EUW1_6002053660, EUW1_6002009239, EUW1_6002003834, EUW1_6001939719, EUW1_6001867883, EUW1_6001022392, EUW1_6000887143, EUW1_6000892356, EUW1_6000787500, EUW1_6000820954, EUW1_5996485374, EUW1_5994559073, EUW1_6010083174, EUW1_6010017420, EUW1_6006898776, EUW1_6006838293, EUW1_6005225782, EUW1_6005135031, EUW1_6003899867, EUW1_6003883079, EUW1_6003786523, EUW1_6002164371, EUW1_6002148723, EUW1_6002057259, EUW1_6002053660, EUW1_6002009239, EUW1_6001022392, EUW1_6000887143, EUW1_6000892356, EUW1_6000787500, EUW1_5999368247, EUW1_5999295110, EUW1_5989231240, EUW1_5989055249, EUW1_5987149834, EUW1_5978125118, EUW1_5969701977, EUW1_5969187233, EUW1_5956294382, EUW1_5955846040, EUW1_5949708234, EUW1_5934525960, EUW1_5916275391, EUW1_5916168691, EUW1_5916132470, EUW1_5907690529, EUW1_5897979620, EUW1_5897921186, EUW1_5896786548, EUW1_5880625543, EUW1_5880541891, EUW1_5878457213, EUW1_6015589872, EUW1_5969852994, EUW1_5969788713, EUW1_5940681289, EUW1_5940556247, EUW1_5937866203, EUW1_5937892773, EUW1_5937768916, EUW1_5937765393, EUW1_5934600651, EUW1_5934512860, EUW1_5934339533, EUW1_5932238516, EUW1_5932166008, EUW1_5932211862, EUW1_5929934534, EUW1_5930153889, EUW1_5931242804, EUW1_5919621815, EUW1_5918236611]

The function which is used for getting the Id for the API call.

/**
 * Getting the Team Members and their IDs
 * 
 * @param {info_id|String}
 * @return Team Members IDs
 * @customfunction
 */
function getTeamMembersIDs(info_id) {
  try {
    startup();
    var teamMembers = getTeamMembersSheet();
    var riotIds = [];
    for (var counter = 0; counter != teamMembers.length; counter = counter   1) {
      switch (info_id){
        case "puuid":
          var data = buildURL("https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/" teamMembers[counter] "?api_key=" apiKey);
          var puuid = data["puuid"];
          riotIds.push(puuid);
          break;
        case "id":
          var data = buildURL("https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/" teamMembers[counter] "?api_key=" apiKey);
          var id = data["id"];
          riotIDs.push(id);
          break;
        case "accountId":
          var data = buildURL("https://euw1.api.riotgames.com/lol/summoner/v4/summoners/by-name/" teamMembers[counter] "?api_key=" apiKey);
          var accountId = data["accountId"];
          riotIds.push(accountId);
          break;
      }
    }
    return riotIds
  } catch (err) {
    Logger.log('Failed with error %s', err.message);
  }
}

This Function is Used for Getting the Matches that got played per Team Member.

 /**
     * Getting the Data from the Api which matches got played
     * 
     * @return the matches played
     * @customfunction
     */
    function getMatchesPlayed() {
      try{
        startup();
        var puuids = getTeamMembersIDs("puuid");
        var games = [];
        for (var counter = 0; counter != puuids.length; counter = counter   1) {
          var data = buildURL("https://europe.api.riotgames.com/lol/match/v5/matches/by-puuid/" puuids[counter] "/ids?start=0&count=20&api_key=" apiKey)
          for(var counter2 = 0; counter2 != data.length; counter2 = counter2   1){
            games.push(data[counter2])
          }
        }
        return games
      }catch (err) {
        Logger.log('Failed with error %s', err.message);
      }
    }

The function which should return the Matching Matches in the Array gets from the getMatchesPlayed function.

/**
 *  Checking the games for matches
 * 
 * @return the matches which match
 * @customfunction
 */
function getMatchingMatches() {
  try{
    var games = getMatchesPlayed();
    var matchingMatches = [];
    Logger.log(games)
    const gettingGames = games.map(element => element)
    for (var counter = 0; counter != games.length; counter = counter   1){
      Logger.log(gettingGames[counter])
    }
  }catch (err) {
    Logger.log('Failed with error %s', err.message);
  }
}

CodePudding user response:

As I understand, you need to get duplicate ids. Use the Set data structure for comparing the array values.

// assume this should return an array [EUW1_6011808396, EUW1_6011824351]
var games = getMatchesPlayed(); // 
var matched_games = []
// Initialise set data structure.
var set = = new Set();

//Iterate over the games. Runtime complexity O(n)
games.forEach((element) => {
   //the case when set does not contain the id we should add it to `set`
   if(!set.has(element)){  // O(1)
       set.add(element);  // O(1)
       continue;
   }

   // if the set already contains the id, we should add it to the matching games array.

   matched_games.push(element);
})

I hope this will help.

CodePudding user response:

welcome to Stackoverflow! You have a lot of ways to store duplicate values In a new array, and this is one.

// get duplicartes and store them in a array
function getDuplicates(array) {
  let sorted = array.sort();
  for (let i = 0; i < sorted.length - 1; i  ) {
    if (sorted[i   1] == sorted[i]) {
      duplicates.push(sorted[i]);
    }
  }
}

CodePudding user response:

Another way to get non-unique values

const euws = ['EUW1_6011808396','EUW1_6011824351','EUW1_6011720277','EUW1_6010413995','EUW1_6010218048','EUW1_6010184913','EUW1_6010131700','EUW1_6009739853','EUW1_6008825456','EUW1_6008833322','EUW1_6008409245','EUW1_6008369887','EUW1_6008355242','EUW1_6007567238','EUW1_6007269146','EUW1_6007226284','EUW1_6007192332','EUW1_6005571988','EUW1_6005438941','EUW1_6005495312','EUW1_6013263286','EUW1_6013193252','EUW1_6012475324','EUW1_6012411610','EUW1_6012315128','EUW1_6012011561','EUW1_6011110477','EUW1_6011026046','EUW1_6009739853','EUW1_6006439870','EUW1_6006434580','EUW1_6005238786','EUW1_6005191249','EUW1_6005026992','EUW1_6005015187','EUW1_6004958241','EUW1_6003811368','EUW1_6002847479','EUW1_6002164371','EUW1_6002148723','EUW1_6015524685','EUW1_6015387328','EUW1_6015402003','EUW1_6014779337','EUW1_6014724668','EUW1_6014701498','EUW1_6014655368','EUW1_6014580839','EUW1_6014429620','EUW1_6014475971','EUW1_6014473252','EUW1_6013334881','EUW1_6013322375','EUW1_6012669749','EUW1_6012635347','EUW1_6012583396','EUW1_6010971941','EUW1_6006896961','EUW1_6006881165','EUW1_6006518887','EUW1_6015745842','EUW1_6015589872','EUW1_6014068520','EUW1_6014044304','EUW1_6007955310','EUW1_6003705297','EUW1_6003569783','EUW1_6002003834','EUW1_6000787500','EUW1_5994465297','EUW1_5993391050','EUW1_5992233473','EUW1_5992169601','EUW1_5984062877','EUW1_5984034743','EUW1_5983855739','EUW1_5983880569','EUW1_5983766086','EUW1_5982931745','EUW1_5982372929','EUW1_6005238786','EUW1_6005191249','EUW1_6005026992','EUW1_6005015187','EUW1_6004958241','EUW1_6002164371','EUW1_6002148723','EUW1_6002057259','EUW1_6002053660','EUW1_6002009239','EUW1_6002003834','EUW1_6001939719','EUW1_6001867883','EUW1_6001022392','EUW1_6000887143','EUW1_6000892356','EUW1_6000787500','EUW1_6000820954','EUW1_5996485374','EUW1_5994559073','EUW1_6010083174','EUW1_6010017420','EUW1_6006898776','EUW1_6006838293','EUW1_6005225782','EUW1_6005135031','EUW1_6003899867','EUW1_6003883079','EUW1_6003786523','EUW1_6002164371','EUW1_6002148723','EUW1_6002057259','EUW1_6002053660','EUW1_6002009239','EUW1_6001022392','EUW1_6000887143','EUW1_6000892356','EUW1_6000787500','EUW1_5999368247','EUW1_5999295110','EUW1_5989231240','EUW1_5989055249','EUW1_5987149834','EUW1_5978125118','EUW1_5969701977','EUW1_5969187233','EUW1_5956294382','EUW1_5955846040','EUW1_5949708234','EUW1_5934525960','EUW1_5916275391','EUW1_5916168691','EUW1_5916132470','EUW1_5907690529','EUW1_5897979620','EUW1_5897921186','EUW1_5896786548','EUW1_5880625543','EUW1_5880541891','EUW1_5878457213','EUW1_6015589872','EUW1_5969852994','EUW1_5969788713','EUW1_5940681289','EUW1_5940556247','EUW1_5937866203','EUW1_5937892773','EUW1_5937768916','EUW1_5937765393','EUW1_5934600651','EUW1_5934512860','EUW1_5934339533','EUW1_5932238516','EUW1_5932166008','EUW1_5932211862','EUW1_5929934534','EUW1_5930153889','EUW1_5931242804','EUW1_5919621815','EUW1_5918236611'];

const nonUnique = euws.reduce((r, v, i, a) => {
    if (!r.includes(v) && a.indexOf(v) !== i) r.push(v);
    return r;
}, []);

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

  • Related