Skip to content

Instantly share code, notes, and snippets.

@kenduigraha
Created May 24, 2020 00:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kenduigraha/5eb1454344f85e3926bd5d3e7b9b716b to your computer and use it in GitHub Desktop.
Save kenduigraha/5eb1454344f85e3926bd5d3e7b9b716b to your computer and use it in GitHub Desktop.
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