-2

I have a logic problem with this javascript, for example

var stone = [
 [1,1,2,1,1],
 [2,3,2,2,2],    
 [4,5,5,5,5],
 [6,6,6,7,6],
 [8,8,8,8,9]
];

How to find the different number and also the position/order of number in the array, and return the variable that contain the information like

var stone = [['number's order','the different number'],[..]];

and the result, variable would be like

var stone = [
 [2,2],
 [1,3],    
 [0,4],
 [3,7],
 [4,9]
];

I look for days to solved and learned this with looping condition "for", but I have lack of basic javascript logic and Im not even close. How can I do this, anyone can help?

I only can reach on this stage, when I tried to break down the index and still looking for the algorithm and stuck

function display(num) {
   $('#test').append($('<div>').text(num));
}

var batu = [
 [1,1,2,1,1],
 [2,3,2,2,2],    
 [4,5,5,5,5],
 [6,6,6,7,6],
 [8,8,8,8,9]
];

for(var i = 0; i < batu.length; i++) {
    var bt = batu[i];
    for(var j = 0; j < bt.length; j++) {
        display("bt[" + i + "][" + j + "] = " + bt[j]);
    }
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="test"></div>

Thanks in advance

Jordyhyde
  • 1
  • 2
  • Check out this post https://stackoverflow.com/questions/9329446/for-each-over-an-array-in-javascript , if your looking for destructuring you can search my username there, i've written some examples. – darklightcode Oct 23 '18 at 07:01
  • 2
    The posted question does not appear to include [any attempt](https://idownvotedbecau.se/noattempt/) at all to solve the problem. StackOverflow expects you to [try to solve your own problem first](https://meta.stackoverflow.com/questions/261592/how-much-research-effort-is-expected-of-stack-overflow-users), as your attempts help us to better understand what you want. Please edit the question to show what you've tried, so as to illustrate a specific problem you're having in a [MCVE]. For more information, please see [ask] and take the [tour]. – CertainPerformance Oct 23 '18 at 07:01
  • hi thanks for remind me to show my attempt, because I don't think its not even close to the answer so I don't have any confident about posting it. @CertainPerformance – Jordyhyde Oct 23 '18 at 07:56
  • @Jordyhyde adding your inputs helps fellow users to know in which direction you are moving so it always helps :) Now you have many answers below to check if you get your solution. Happy Coding – Nitish Narang Oct 23 '18 at 08:02

3 Answers3

2

You can use "map" and "reduce" to achieve your desired result as below

var stone = [
 [1,1,2,1,1],
 [2,3,2,2,2],    
 [4,5,5,5,5],
 [6,6,6,7,6],
 [8,8,8,8,9]
];

var getUniqueElementWithIndex = (arr) => 
                  arr.reduce((a,d,i) => (
                    a.length == 0 
                      && arr.indexOf(d) == arr.lastIndexOf(d) 
                      && (a = [i, d])
                    , a) 
                  , [])

var result = stone.map(getUniqueElementWithIndex)

console.log(result)
Nitish Narang
  • 4,124
  • 2
  • 15
  • 22
  • Thanks a lot, I'm not even know that there are "map" and "reduce" method before, will looking into it – Jordyhyde Oct 23 '18 at 08:02
  • You're welcome. Please do have a good understanding of the se two methods as these are most used methods. Also let me know if you face any issue with this solution. – Nitish Narang Oct 23 '18 at 08:03
0

You could find first the common value and then look for the different value with it's index. if found exit the loop and return value and index.

var stone = [[1, 1, 2, 1, 1], [2, 3, 2, 2, 2], [4, 5, 5, 5, 5], [6, 6, 6, 7, 6], [8, 8, 8, 8, 9]],
    missing = stone.map(a => {
        var r = a[0] === a[1] ? a[0] : a[2],
            result;
        a.some((v, i) => v !== r && (result = [i, v]));
        return result;
    });

console.log(missing)
Rahul
  • 18,271
  • 7
  • 41
  • 60
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
0

the below is a very simple way of doing without map and reduce , it also has a way t0 add it in the 2D array.

 var stone = [
        [1, 1, 2, 1, 1],
        [2, 3, 2, 2, 2],
        [4, 5, 5, 5, 5],
        [6, 6, 6, 7, 6],
        [8, 8, 8, 8, 9]
    ];
    var position = [], diffrent = [];
    var array = [
        [0,0],
        [0,0],
        [0,0],
        [0,0],
        [0,0]
    ];
    for (var i = 0; i < stone.length; i++)
        for (var j = 0; j < stone.length - 1; j++) {
            if (stone[i][j] !== stone[i][j + 1]) {
                diffrent[i] = stone[i][j];
                position[i] = j;
            }
        }
    for (var i = 0; i < stone.length; i++) {
        console.log("Position is :\t" + position[i] + "\tand the diffrent Element is \t" + diffrent[i]);
        array[i][0] =  position[i];
        array[i][1] =  position[i];
    }
Dark_thunder
  • 143
  • 1
  • 1
  • 11