Skip to content

Commit

Permalink
remove string question and add question: 'findMostFrequent()' which f…
Browse files Browse the repository at this point in the history
…inds the element in an array which occurs the most
  • Loading branch information
Gina Benavidez authored and Gina Benavidez committed Aug 10, 2017
1 parent 173af0b commit 496f67c
Showing 1 changed file with 29 additions and 7 deletions.
36 changes: 29 additions & 7 deletions Arrays/writtenInJavascript.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,34 @@ function mergeTwoArrays(arr){

mergeTwoArrays([ ['b', 'a'], ['l', 'z', 'y'] ]);

// - Find out if a given string is a palindrome
function palindrome(str){
if (str.toLowerCase() === str.toString().toLowerCase().split('').reverse().join('')){
return true;
} else {
return false;
// given an array of integers, find the number that is repeated the most.
function findMostFrequent(arr){
var repsCount = {};
for(i=0; i<arr.length; i++){
if(!repsCount[arr[i]]){
repsCount[arr[i]] = 1;
} else {
repsCount[arr[i]] = repsCount[arr[i]] + 1;
}
}
var occursMore = [];
for (var key in repsCount){
if (repsCount[key] > occursMore){
occursMore.push(key);
}
}
return occursMore;
}
// I'd especially like to see other approaches to this problem, comment if you have a different approach.

// - Find all even numbers in array
function evenOnly(array){
var onlyEven = [];
for (i=0; i<array.length; i++){
if(array[i] % 2 === 0){
onlyEven.push(array[i]);
}
}
return onlyEven;
}
gina

0 comments on commit 496f67c

Please sign in to comment.