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
- Create four array containers i.e. c1, c2, c3 and c4.
- Put above data in c1 container as it is
loop through c1 and put the data in c4 in following format
"109", "200", "109", "201", "102", "202", "103", "202"loop through c1 and put them in c2 in this format
"109,200"then check if c3 is empty then read first value from c2 and push it in c3.
repeat step 4 but this time put second data i.e. "109 201" from c1 in c2 in this format
"109,201"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);