Open ErSKS opened 5 years ago
package dualarray;
/**
@College: Khwopa College of Engineering */ public class DualArray {
public static void main(String[] args) { System.out.println("Is Dual {1, 4, 4, 3, 3, 5, 5, 1} 1:" + isDual(new int[]{1, 4, 4, 3, 3, 5, 5, 1})); System.out.println("Is Dual {1, 4, 4, 3, 9, 5, 5, 1} 0:" + isDual(new int[]{1, 4, 4, 3, 9, 5, 5, 1})); System.out.println("Is Dual {1, 4, 8, 3, 3, 5, 5} 0:" + isDual(new int[]{1, 4, 8, 3, 3, 5, 5})); System.out.println("Is Dual {1, 4, 3, 3, 3, 5, 5, 1} 0:" + isDual(new int[]{1, 4, 3, 3, 3, 5, 5, 1})); System.out.println("Is Dual {1, 4, 4, 9, 9, 5, 5, 1} 1:" + isDual(new int[]{1, 4, 4, 9, 9, 5, 5, 1})); System.out.println("Is Dual {1, 4, 4, 3, 3, 5, 5, 1} 1:" + isDual(new int[]{1, 4, 4, 3, 3, 5, 5, 1})); System.out.println("Is Dual {1, 4, 6, 3, 3, 6, 5, 1} 0:" + isDual(new int[]{1, 4, 6, 3, 3, 6, 5, 1})); System.out.println("Is Dual {1, 4, 4, 3, 3, 5, 5, 1} 1:" + isDual(new int[]{1, 4, 4, 3, 3, 5, 5, 1})); System.out.println("Is Dual {} 1:" + isDual(new int[]{})); }
private static int isDual(int[] i) { int count = 0; if (i.length % 2 == 1) { return 0; } for (int j = 0; j < i.length; j++) { count = 0; for (int k = 0; k < i.length; k++) { if (i[j] == i[k]) { count++; } } if (count != 2) { return 0; } } return 1; } }
CoreTask#4: DualArray - Define a Dual array to be an array where every value occurs exactly twice. For example, {1, 2, 1, 3, 3, 2} is a dual array. The following arrays are not Dual arrays {2, 5, 2, 5, 5} (5 occurs three times instead of two times) {3, 1, 1, 2, 2} (3 occurs once instead of two times) Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 0. Function signature is int isDual (int[ ] a)
Dual array Define a Dual array to be an array where every value occurs exactly twice. For example, {1, 2, 1, 3, 3, 2} is a dual array.The following arrays are not Dual arrays {2, 5, 2, 5, 5} (5 occurs three times instead of two times) {3, 1, 1, 2, 2} (3 occurs once instead of two times) Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 0.
Hi How are you doing? I am good. can you do it Dual array in JavaScript? Thank you for your help. Jos
//Using JavaScript methods: function isDual(arr){ if (arr.length % 2 !== 0) { return 0; } else{ let sortedArray = arr.sort((a, b) => a - b ); let evenIndex = arr.filter((value,i) => i %2 === 0) let oddIndex = arr.filter((value,i) => i %2 !== 0) let compare = evenIndex.map((value, i) => value == oddIndex[i]) if (compare.every((n) => n === true)){ return 1; } } } console.log(isDual([1, 2, 1, 3, 3, 2])) //output 1 console.log(isDual([2, 5, 2, 5, 5])) //output 0 console.log(isDual([3, 1, 1, 2, 2])) //putput 0
//Using JavaScript
let isDual = (array) => { var count = 0; if (array.length % 2 == 1) { return 0; }
for (j = 0; j < array.length; j++) { count = 0; for (k = 0; k < array.length; k++) { if (array[j] == array[k]) { count++; } } if (count != 2) { return 0; } } return 1; }
console.log(isDual([1, 2, 1, 3, 3, 2, 1, 1, 5, 6, 5, 6,])); //return 0 since it have more than 2 the same num
console.log(isDual([1, 2, 1, 3, 3, 2])); console.log(isDual([2, 5, 2, 5, 5]));
console.log(isDual([3, 1, 1, 2, 2]));
CoreTask#4: DualArray - Define a Dual array to be an array where every value occurs exactly twice. For example, {1, 2, 1, 3, 3, 2} is a dual array. The following arrays are not Dual arrays {2, 5, 2, 5, 5} (5 occurs three times instead of two times) {3, 1, 1, 2, 2} (3 occurs once instead of two times) Write a function named isDual that returns 1 if its array argument is a Dual array. Otherwise it returns 0. Function signature is int isDual (int[ ] a)