Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8399803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:24:12+00:00 2026-06-09T21:24:12+00:00

I have made small algorithm and want to implement it using javascript. Here is

  • 0

I have made small algorithm and want to implement it using javascript. Here is my algorithm

I have a data in data.json file in this format

[
"109 200",
"109 201",
"102 202",
"103 202"
]

What I am trying to do is

  1. Create four array containers i.e. c1, c2, c3 and c4.
  2. Put above data in c1 container as it is
  3. loop through c1 and put the data in c4 in following format

    "109",
    "200",
    "109",
    "201",
    "102",
    "202",
    "103",
    "202"
    
  4. loop through c1 and put them in c2 in this format

    "109,200"
    
  5. then check if c3 is empty then read first value from c2 and push it in c3.

  6. repeat step 4 but this time put second data i.e. “109 201” from c1 in c2 in this format

    "109,201"
    
  7. then check if c3 is not empty then loop through c2 and check if any of these two values are repeated in c4. If it is repeated then repeat step 6 and 7 until it finds least amount of numbers from data.json.

This algorithm is not efficient but still I want to use this.

Here is my code.

var teams = [],
    c1 = [], arr = [], left = [], right = [], j = 0,
    show = function () {
        var span = $('#list');

        $.getJSON('data/data.json', function (ids) {

            //c1.push(c);
            for (var i = 0; i < ids.length; i++) {
                var a = smallcontainer(ids);
                var b = bigcontainer(ids);
                var c;

                if (c1 == "") {
                    c = a[0].split(" ");
                    console.log(c);
                } else {

                }


                //console.log(c);
                var id = ids[i];
                teams = id;
                $('<li>' + id + '</li>').appendTo(span);
            }
        });
    },

    smallcontainer = function (teams) { //arr
        arr = [teams[j]];
        j++;
        return arr;
    },

    bigcontainer = function (ids) { //c3. in code it is left+right=result
        for (var i = 0; i < ids.length; i++) {
            var splitted = ids[i].split(" ");
            left.push(splitted[0]);
            right.push(splitted[1]);

        }
        var result = left.concat(right);
    };

Update

data inside data.json file has four teams with two members in each team in this form

"109 200" = Team 1
"109 201" = Team 2
"102 202" = Team 3
"103 202" = Team 4

So now I have to compute the smallest number of people and it has to select one member from each team from this list and show their IDs. So the output for above would be

109
202

Latest update

I am still waiting for help

Solution

Here is the solution with the help of AlexBEll and PAEz. I used solution below which was basically solved by AlexBell

var data = [
 "1009 2000", 
 "1009 2001", 
 "1002 2002", 
 "1003 2002", 
 "1004 2003", 
 "1005 2004", 
 "1006 2005", 
 "1007 2006", 
 "1007 2007", 
 "1008 2008", 
 "1009 2008", 
 "1010 2009", 
 "1011 2010", 
 "1012 2010"      
];

var first = [], second = [], result = {}, out = '';

//Separe the ids
for(var i = 0; i < data.length; i++){
    var el = data[i].split(' '); 
    first[i] = el[0];
    second[i] = el[1];
}

for(var k = 0; k < first.length; k++){

    //Count the elements
    var nOfFirst = countElemnts(first, first[k]);
    var nOfSecond = countElemnts(second, second[k]);

    //If the first is in more that second take it
    if(nOfFirst > nOfSecond){
        result[first[k]] = 0;
    //Else take the second        
    }else if(nOfFirst < nOfSecond){
        result[second[k]] = 0;
    //If is only one take it    
    }else{
        result[first[k]] = 0;
    }

}

function countElemnts(arr, el){
    var count = 0;
    for(var j = 0; j < arr.length; j++){
        if(arr[j] == el)
            count++;
    }
    //console.log(el+' : '+count);
    return count;
}

for(var n in result){
    out += 'The id n: '+n+' is in the list\n';
}

alert(out);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T21:24:13+00:00Added an answer on June 9, 2026 at 9:24 pm

    Does this work?….

    var teams=[
    "109 200",
    "109 201",
    "102 202",
    "103 202"
    ];
    
    var members ={};
    
    var matesId='109';
    
    // Members that won
    var wins={};
    
    // First lets find out how many teams a member is in
    for (var i=0,length=teams.length; i<length;i++){
      var temp = teams[i].split(' ');
      for (var z=0,zlength=temp.length;z<zlength;z++){
        if (!members[temp[z]]) members[temp[z]]={wins:0,totalTeams:0,id:temp[z]};
          members[temp[z]].totalTeams=members[temp[z]].totalTeams+1;
      }
        teams[i]=[members[temp[0]],members[temp[1]]];
    }
    
    for (var i=0,length=teams.length; i<length;i++){
      var member1=teams[i][0];
      var member2=teams[i][1];
      if (member1.totalTeams>member2.totalTeams){
        member1.wins=member1.wins+1;
      } else if (member1.totalTeams<member2.totalTeams){
        member2.wins=member2.wins+1;
      } else {
        member1.wins=member1.wins+1;
        member2.wins=member2.wins+1;
      }    
    }
    
    for (var i=0,length=teams.length; i<length;i++){
      var member1=teams[i][0];
      var member2=teams[i][1];
      if (member1.wins>member2.wins){
        if (wins[member2.id]!==true) wins[member1.id]=true;
      } else if (member1.wins<member2.wins){
        if (wins[member1.id]!==true) wins[member2.id]=true;
      } else if (!wins[member1.id] && !wins[member2.id]) {
        if (member1.id==matesId && member2.id==matesId) {
          wins[matesId]=true;
        } else{
         // A draw, so pick one
          Math.round(Math.random())==1 ? wins[member2.id]=true : wins[member1.id]=true;
        }
      }    
    }
    
    var keys=Object.keys(wins);
    var results=[];
    results.push(keys.length);
    for (var i=0,length=keys.length; i<length;i++){
      results.push(keys[i]);
    }
    results=results.join('\n');
    document.querySelector('#output').innerText=results;​
    

    http://jsfiddle.net/PAEz/dLUqj/3/
    EDIT: Updated it so its a little easier to read.
    EDIT: Realised you dont need a draw and win, just a win will do.
    LAST EDIT: Noticed one small error, it should all be right now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a small log service that i want to publish to a
i have made a small opengl program using the d programming language. what i
I have made a small program in C# that I want to run in
I appologize for this lengthy post. I have made it as small as possible
I am new to .NET, I have made a small app. I want that
I have made a small xslt file to create an html output called weather.xsl
I have to implement an algorithm on data which is (for good reasons) stored
I have made a small webserver as a mock for my JUnit tests. this
I have made a small java swing application that I want to share with
I have actually a small problem with file i/o and finding the right algorithm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.