Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added .DS_Store
Binary file not shown.
Binary file added Week1/.DS_Store
Binary file not shown.
Binary file added Week1/assignments/.DS_Store
Binary file not shown.
Binary file added Week1/assignments/armand-collins/.DS_Store
Binary file not shown.
29 changes: 29 additions & 0 deletions Week1/assignments/armand-collins/Dominator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@


const dominator = (arr) => {
let values = {};
let maxValue = arr.length/2
let dominator;

for(let val of arr) {
if(values[val] == null) {
values[val] = 1;
} else {
values[val]++;
}
}

for(let count of arr) {
if(values[count] > maxValue) {
dominator = count;
}
}

if(dominator) {
return arr.indexOf(dominator);
}
return -1
}

console.log(dominator([3, 4, 2, 3, 3, 2, -1, 3, 3]));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@fogha your function doesn't return the required values.

The question clearly states: returns the index of any element of array A in which the dominator of A occurs. The function should return −1 if array A does not have a dominator.

console.log(dominator([3, 4, 2, 3, 2, 2, -1, 3, 3]));