An array A consisting of N integers is given. The dominator of array A is the value that occurs in more than half of the elements of A.
For example, consider array A such that
A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3The dominator of A is 3 because it occurs in 5 out of 8 elements of A (namely in those with indices 0, 2, 4, 6 and 7) and 5 is more than a half of 8.
Write a function
function solution(A);
that, given an array A consisting of N integers, returns 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.
For example, given array A such that
A[0] = 3 A[1] = 4 A[2] = 3 A[3] = 2 A[4] = 3 A[5] = -1 A[6] = 3 A[7] = 3the function may return 0, 2, 4, 6 or 7, as explained above.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [0..100,000];
- each element of array A is an integer within the range [−2,147,483,648..2,147,483,647].
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
[ 3, 4, 3, 2, 3, -1, 3, 3 ]
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{}
{}
{}
{}
{}
{}
{}
{}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = {};
A.reduce((acc,v,i)=>{
console.log(map,v,i)
map[v] ? map[v].push(i) : [i];
});
console.log(map)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{} 4 1
{} 3 2
{} 2 3
{} 3 4
{} -1 5
{} 3 6
{} 3 7
{}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = {};
A.reduce((acc,v,i)=>{
console.log(map,v,i)
map[v] ? map[v].push(i) : map[v] = [i];
});
console.log(map)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = {};
A.reduce((acc,v,i)=>{
console.log(map,v,i)
map[v] ? map[v].push(i) : map[v] = [i];
});
console.log(map)
}
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{} 4 1
{ '4': [ 1 ] } 3 2
{ '3': [ 2 ], '4': [ 1 ] } 2 3
{ '2': [ 3 ], '3': [ 2 ], '4': [ 1 ] } 3 4
{ '2': [ 3 ], '3': [ 2, 4 ], '4': [ 1 ] } -1 5
{ '2': [ 3 ], '3': [ 2, 4 ], '4': [ 1 ], '-1': [ 5 ] } 3 6
{ '2': [ 3 ], '3': [ 2, 4, 6 ], '4': [ 1 ], '-1': [ 5 ] } 3 7
{ '2': [ 3 ], '3': [ 2, 4, 6, 7 ], '4': [ 1 ], '-1': [ 5 ] }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '2': [ 3 ], '3': [ 2, 4, 6, 7 ], '4': [ 1 ], '-1': [ 5 ] }
Invalid result type, integer expected, 'undefined' found Perhaps you are missing a 'return'?stdout:
{ '2': [ 3 ], '3': [ 0, 2, 4, 6, 7 ], '4': [ 1 ], '-1': [ 5 ] }
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
console.log(map)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
}
console.log(map)
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(item > A.length/2)
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(item > A.length/2) return
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item];
}
return -1;
}
Invalid result type, integer expected, 'object' found
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) console.;
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) console.log(map[item]);
}
return -1;
}
[ 0, 2, 4, 6, 7 ]
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return ...map[item];
}
return -1;
}
solution.js:12
if(map[item].length > A.length/2) return ...map[item];
^^^
SyntaxError: Unexpected token ...
at createScript (vm.js:80:10)
at Object.runInNewContext (vm.js:135:10)
at getSolution (/tmp/exec.js:392:29)
at Promise.resolve.then (/tmp/exec.js:426:34)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return (...map[item]);
}
return -1;
}
solution.js:12
if(map[item].length > A.length/2) return (...map[item]);
^
SyntaxError: Unexpected token [
at createScript (vm.js:80:10)
at Object.runInNewContext (vm.js:135:10)
at getSolution (/tmp/exec.js:392:29)
at Promise.resolve.then (/tmp/exec.js:426:34)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
at Function.Module.runMain (module.js:686:11)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item];
}
return -1;
}
Invalid result type, integer expected, 'object' found
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item][];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item][0];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item][0];
}
return -1;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
const map = A.reduce((map,v,i)=>{
map[v] ? map[v].push(i) : map[v] = [i];
return map;
},{});
for(let item in map){
if(map[item].length > A.length/2) return map[item][0];
}
return -1;
}
The solution obtained perfect score.