Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

swapping elements in an array

let a = 1;
let b = 3;

[a, b] = [b, a]; // destructuring assignment, assigning the value of a and b to a new array created with b and a
console.log(a); // 3
console.log(b); // 1

const arr = [1,2,3];
[arr[2], arr[1]] = [arr[1], arr[2]];
console.log(arr); // [1,3,2]
Comment

swap in array

import java.util.Arrays;

public class swapInArray {

    public static void main (String[] args) {
                int[] arr = {1,10,100,1000};
                swap(arr,1,3);
                System.out.println(Arrays.toString(arr));
            }

            private static void swap (int[] arr, int index1, int index2){
                int elem1 = arr[index1];
                int elem2 = arr[index2];
                arr[index1] = elem1;
                arr[index2] = elem2;
    }
}
Comment

PREVIOUS NEXT
Code Example
Javascript :: get child routes using parent in angular 
Javascript :: string to binary javascript 
Javascript :: get parent id javascript 
Javascript :: jspdf add page 
Javascript :: isarray 
Javascript :: angular input value 
Javascript :: google script wait 
Javascript :: get size of json object 
Javascript :: dom click is not a function 
Javascript :: javascript check if json file is empty 
Javascript :: angular event emitter 
Javascript :: usestate array delete 
Javascript :: react native get mac address 
Javascript :: jquery remove items from dropdownlist 
Javascript :: js is numeric 
Javascript :: get id by this jquery 
Javascript :: time calculator js 
Javascript :: jquery array 
Javascript :: flutter access json object inside object 
Javascript :: jquery validation submit handler 
Javascript :: difference between == and === in javascript 
Javascript :: two decimel javascript 
Javascript :: javascript get dictionary values 
Javascript :: jquery on hover dynamic elements 
Javascript :: javascript undefined check 
Javascript :: javascript run two functions at the same time 
Javascript :: copy to clipboard js 
Javascript :: handle error in promise chain 
Javascript :: custom login with facebook button react native 
Javascript :: regExp numero français 
ADD CONTENT
Topic
Content
Source link
Name
9+5 =