Check if two arrays contain the same elements?
Example:
Input: arr1 = { 4, 6, 1, 8, 1 }
Output: arr2 = { 4, 6, 1, 8, 1 }
Approach: int array
Java
import java.util.Arrays;public class CopyArrayTo {public static void main(String[] args) {// original arrayint[] arr1 = new int[] { 4, 6, 1, 8, 1 };// new array of original array sizeint arr2[] = new int[arr1.length];// copy using loopfor (int i = 0; i < arr1.length; i++) {arr2[i] = arr1[i];}System.out.println("Orginal Array " + Arrays.toString(arr1));System.out.println("Copy Array " + Arrays.toString(arr2));}}
No comments:
Post a Comment