Created
May 24, 2020 00:09
-
-
Save kenduigraha/5eb1454344f85e3926bd5d3e7b9b716b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let solution = (relation) => { | |
let answer = 0; | |
let dataRow = []; | |
let findDuplicates = (arr) => | |
arr.filter((item, index) => arr.indexOf(item) != index); | |
// - Uniqueness: The relation does not have two distinct tuples | |
// (i.e. rows or records in common database language) | |
// with the same values for these attributes. | |
for (let i = 0; i < relation[0].length; i++) { | |
const rowData = relation.map((data) => data[i]); | |
const uniqueDuplicates = [...new Set(findDuplicates(rowData))]; | |
if (uniqueDuplicates.length === 0) { | |
dataRow.push(i); | |
answer++; | |
} | |
} | |
// checking value in data array | |
for (let i = 0; i < relation[0].length; i++) { | |
for (let j = 0; j < relation[0].length; j++) { | |
const isCurrUnique = dataRow.findIndex((dataCurr) => dataCurr === i) > -1; | |
const isNextUnique = dataRow.findIndex((dataNext) => dataNext === j) > -1; | |
// prevent same value | |
if (i !== j) { | |
if (!isCurrUnique && !isNextUnique) { | |
const oneRows = relation.map((data) => `${data[i]} ${data[j]}`); | |
const isDuplicatedCol = [...new Set(findDuplicates(oneRows))]; | |
if (isDuplicatedCol.length === 0) { | |
dataRow.push(i); | |
dataRow.push(j); | |
answer++; | |
} | |
} | |
} | |
} | |
} | |
return answer; | |
}; | |
// [Student number, Name, Major, Grade] | |
let relation = [ | |
["100", "ryan", "music", "2"], | |
["200", "apeach", "math", "2"], | |
["300", "tube", "computer", "3"], | |
["400", "con", "computer", "4"], | |
["500", "muzi", "music", "3"], | |
["600", "apeach", "music", "2"], | |
]; | |
// answer: 2 | |
console.log(solution(relation)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment