KCE / Java

Java Programming Language
0 stars 3 forks source link

CoreTask#4: DualArray - Submit Your Code in Comment Section #29

Open ErSKS opened 5 years ago

ErSKS commented 5 years ago

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)

Niranjan2054 commented 5 years ago

package dualarray;

/**

tg199031 commented 3 years ago

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)

tg199031 commented 3 years ago

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.

yosephhaile23 commented 2 years ago

Hi How are you doing? I am good. can you do it Dual array in JavaScript? Thank you for your help. Jos

Abenezer-Baheru commented 1 year ago

//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

Daniel0402abera commented 1 year ago

//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]));