Counting and deleting multiple Elements in a nested Array - javascript

this is the structure of my array:
arrayParent = [numbers, counter];
numbers = [1,1,1,2,4,5];
counter = [];
what I want to do is counting multiple elements in "numbers" and pushing to "counter" while deleting in the first, in the end it should look like this:
numbers = [1,2,4,5];
counter = [3,1,1,1];
I tried this ( and many many other versions):
for(var y =0; y < arrayParent.length; y++){
for(var x = 0; x < arrayParent[y].numbers.length; x++){
var co = 1;
for(var z = x+1; z < arrayParent[y].numbers.length; z++){
if(arrayParent[y].numbers[x] == arrayParent[y].ans[z]){
co++;
arrayParent[y].numbers.splice(z);
arrayParent[y].counter[x] = co;
}
}
}
}
The result I got:
numbers = [1,2,4,5];
counter = [3,,,];
Any ideas how to solve?

you can try something like:
let numbers = [1,1,1,2,4,5];
let counter = [];
const tmp = numbers.reduce((res, curr) => ({
...res,
[curr]: (res[curr] || 0) + 1
}), {});
numbers = Object.keys(tmp).map(Number);
counter = Object.values(tmp);
console.log(numbers, counter);
so, I created a counter object where keys are distinct numbers and values are their counter
As #nikhil correctly noticed this method will not preserve numbers order, to preserve it, just change JS object to JS Map, the logic is the same:
let numbers = [1,1,1,2,5, 4];
let counter = [];
const tmp = numbers.reduce((res, curr) => res.set(curr, (res.get(curr) || 0) + 1), new Map());
numbers = [...tmp.keys()];
counter = [...tmp.values()];
console.log(numbers, counter);

You can try following
let arrayParent = {numbers : [1,1,1,2,4,5], counter : []};
// This will ensure that the order of elements is same as in the array
let order = [];
// Create an object with key as `number` and counts as its value
let map = arrayParent.numbers.reduce((a,c) => {
if(a[c]) a[c]++;
else {a[c] = 1; order.push(c); }
return a;
}, new Map());
// Set the value of order in numbers
arrayParent.numbers = order;
// Set the counts in counter
order.forEach(o => arrayParent.counter.push(map[o]));
console.log(arrayParent);

Related

Array elements not mutating inside for of loop

function rot13(str) {
let alphArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let n = 13;
let arr = str.split("");
let len = alphArr.length;
for (let i of arr) {
if (alphArr.includes(i)) {
if (alphArr.indexOf(i) + n <= len - 1) {
i = (alphArr[alphArr.indexOf(i) + n])
console.log(i) // This is as expected
}
}
}
console.log(arr) // Array itself did not mutate and is showing the initial array.
return str;
}
rot13("SERR PBQR PNZC");
The value of i inside the second if statement is proper as can be seen in the console.log statement but the array in itself did not mutate. Why was that?
P.S. I've solved it by using map function and it works properly because map function does not mutate the original array.
It's worth mentioning that your code can be simplified:
let rot = (str, n, asciiStart='A'.charCodeAt(0), asciiEnd='Z'.charCodeAt(0), asciiRange=asciiEnd-asciiStart+1) =>
str.split('')
.map(c => {
let code = c.charCodeAt(0) - asciiStart;
if (code >= 0 && code <= asciiRange) code = (code + n) % asciiRange;
return String.fromCharCode(asciiStart + code);
})
.join('');
let inp = document.getElementsByTagName('input')[0];
let p = document.getElementsByTagName('p')[0];
inp.addEventListener('input', () => p.innerHTML = rot(inp.value, 13));
<input type="text" placeholder="test here (use capital letters)"/>
<p></p>
Your code wasn't working because replacing the value of i does not effect the array index that i was initially based off of. Once you define i, it doesn't remember how it was defined (e.g. it doesn't think to itself, "I originated from a value within an array')
You can't directly set value to element in for of loop try the following...
function rot13(str) {
let alphArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let n = 13;
let arr = str.split("");
let len = alphArr.length;
let j=0
for (let i of arr) {
if (alphArr.includes(i)) {
if (alphArr.indexOf(i) + n <= len - 1) {
arr[j]= (alphArr[alphArr.indexOf(i) + n])
console.log(i) // This is as expected
}
}
j++
}
console.log(arr) // Array itself did not mutate and is showing the initial array.
return str;
}
rot13("SERR PBQR PNZC");
Instead of using a for of loop you should use map to create a new array and used the newly mapped array. Fixed fulling working example:
function rot13(str) {
let alphArr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
let n = 13;
let arr = str.split("");
let len = alphArr.length;
arr = arr.map((i) => {
if (alphArr.includes(i)) {
let index = (alphArr.indexOf(i) + n) % len;
return alphArr[index];
}
return i;
});
return arr.join("");
}
console.log(rot13("SERR PBQR PNZC")); // logs "FREE CODE CAMP"

Print elements of object array in Javascript

I have this array of objects to count element frequency in another array using for loop which prints correct output.
counts = {};
counter = 0;
counter_array = [50,50,0,200]; //this is just for example, this array is filled dynamically
for (var x = 0, y = counter_array.length; x < y; x++) {
counts[counter_array[x]] = (counts[counter_array[x]] || 0) + 1;
}
console.log('FREQUENCY: ',counts); //outputs FREQUENCY: {50:2, 0:1, 200:1}
There is another array of arrays:
holder_text_array = [["a",50,0],["b",0,0]]; //example of dynamically filled array
var p = "a";
var i = 0;
while(i < holder_text_array.length){
if (holder_text_array[i][0]==p) {
var s = counts[holder_text_array[i][1]];
console.log('Element: ', holder_text_array[i][1]); //prints 50 for i = 0
console.log('frequency: ',counts[s]); //prints undefined
counter = counts[s];
}
i++;
}
The array of arrays "holder_text_array" consists of elements whose frequency I need to get in the while loop. Can someone tell me where am I wrong?
The frequency is stored in s not in counts[s]
You're logging counts[s] where var s = counts[holder_text_array[i][1]];
You've already got the element from counts in s. Just log the value of s
Apart from that the function works!
counts = {};
counter = 0;
counter_array = [50,50,0,200]; //this is just for example, this array is filled dynamically
for (var x = 0, y = counter_array.length; x < y; x++) {
counts[counter_array[x]] = (counts[counter_array[x]] || 0) + 1;
}
console.log('FREQUENCY: ',counts); //outputs FREQUENCY: {50:2, 0:1, 200:1}
holder_text_array = [["a",50,0],["b",0,0]]; //example of dynamically filled array
var p = "a";
var i = 0;
while(i < holder_text_array.length){
if (holder_text_array[i][0]==p) {
var s = counts[holder_text_array[i][1]];
console.log('Element: ', holder_text_array[i][1]); //prints 50 for i = 0
console.log('frequency: ', s); // CHANGED THIS TO JUST `s`
counter = counts[s];
}
i++;
}
You could take a recursive approach and call the count function again for (nested) arrays with the same counts object.
The result contains the counts of each element.
function getCounts(array, counts = {}) {
for (let i = 0; i < array.length; i++) {
const value = array[i];
if (Array.isArray(value)) {
getCounts(value, counts);
continue;
}
if (!counts[value]) counts[value] = 0;
counts[value]++;
}
return counts;
}
console.log(getCounts([["a", 50, 0], ["b", 0, 0]]));
I figured out the problem. Issue is in initialization.
I changed the following:
var s = counts[holder_text_array[i][1]];
counter = counts[s];
It works this way:
var s = holder_text_array[i][1];
counter = counts[s];

Common Character Count in Strings JavaScript

Here is the problem:
Given two strings, find the number of common characters between them.
For s1 = "aabcc" and s2 = "adcaa", the output should be 3.
I have written this code :
function commonCharacterCount(s1, s2) {
var count = 0;
var str = "";
for (var i = 0; i < s1.length; i++) {
if (s2.indexOf(s1[i]) > -1 && str.indexOf(s1[i]) == -1) {
count++;
str.concat(s1[i])
}
}
return count;
}
console.log(commonCharacterCount("aabcc", "adcaa"));
It doesn't give the right answer, I wanna know where I am wrong?
There are other more efficient answers, but this answer is easier to understand. This loops through the first string, and checks if the second string contains that value. If it does, count increases and that element from s2 is removed to prevent duplicates.
function commonCharacterCount(s1, s2) {
var count = 0;
s1 = Array.from(s1);
s2 = Array.from(s2);
s1.forEach(e => {
if (s2.includes(e)) {
count++;
s2.splice(s2.indexOf(e), 1);
}
});
return count;
}
console.log(commonCharacterCount("aabcc", "adcaa"));
You can do that in following steps:
Create a function that return an object. With keys as letters and count as values
Get that count object of your both strings in the main function
Iterate through any of the object using for..in
Check other object have the key of first object.
If it have add the least one to count using Math.min()
let s1 = "aabcc"
let s2 = "adcaa"
function countChars(arr){
let obj = {};
arr.forEach(i => obj[i] ? obj[i]++ : obj[i] = 1);
return obj;
}
function common([...s1],[...s2]){
s1 = countChars(s1);
s2 = countChars(s2);
let count = 0;
for(let key in s1){
if(s2[key]) count += Math.min(s1[key],s2[key]);
}
return count
}
console.log(common(s1,s2))
After posting the question, i found that i havent looked the example well. i thought it wants unique common characters ..
and i changed it and now its right
function commonCharacterCount(s1, s2) {
var count = 0;
var str="";
for(var i=0; i<s1.length ; i++){
if(s2.indexOf(s1[i])>-1){
count++;
s2=s2.replace(s1[i],'');
}
}
return count;
}
Create 2 objects containing characters and their count for strings s1
and s2
Count the common keys in 2 objects and return count - Sum the common keys with minimum count in two strings
O(n) - time and O(n) - space complexities
function commonCharacterCount(s1, s2) {
let obj1 = {}
let obj2 = {}
for(let char of s1){
if(!obj1[char]) {
obj1[char] = 1
} else
obj1[char]++
}
for(let char of s2){
if(!obj2[char]) {
obj2[char] = 1
} else
obj2[char]++
}
console.log(obj1,obj2)
let count = 0
for(let key in obj1 ){
if(obj2[key])
count += Math.min(obj1[key],obj2[key])
}
return count
}
I think it would be a easier way to understand. :)
function commonCharacterCount(s1: string, s2: string): number {
let vs1 = [];
let vs2 = [];
let counter = 0;
vs1 = Array.from(s1);
vs2 = Array.from(s2);
vs1.sort();
vs2.sort();
let match_char = [];
for(let i = 0; i < vs1.length; i++){
for(let j = 0; j < vs2.length; j++){
if(vs1[i] == vs2[j]){
match_char.push(vs1[i]);
vs2.splice(j, 1);
break;
}
}
}
return match_char.length;
}
JavaScript ES6 clean solution. Use for...of loop and includes method.
var commonCharacterCount = (s1, s2) => {
const result = [];
const reference = [...s1];
let str = s2;
for (const letter of reference) {
if (str.includes(letter)) {
result.push(letter);
str = str.replace(letter, '');
}
}
// ['a', 'a', 'c'];
return result.length;
};
// Test:
console.log(commonCharacterCount('aabcc', 'adcaa'));
console.log(commonCharacterCount('abcd', 'aad'));
console.log(commonCharacterCount('geeksforgeeks', 'platformforgeeks'));
Cause .concat does not mutate the string called on, but it returns a new one, do:
str = str.concat(s1[i]);
or just
str += s1[i];
You can store the frequencies of each of the characters and go over this map (char->frequency) and find the common ones.
function common(a, b) {
const m1 = {};
const m2 = {};
let count = 0;
for (const c of a) m1[c] = m1[c] ? m1[c]+1 : 1;
for (const c of b) m2[c] = m2[c] ? m2[c]+1 : 1;
for (const c of Object.keys(m1)) if (m2[c]) count += Math.min(m1[c], m2[c]);
return count;
}

Start a for loop on specific index and loop for array length

I'm trying to do a for loop on an array and be able to start that loop on a specific index and loop over the array x amount of times.
const array = ['c','d','e','f','g','a','b','c']
I want to loop 8 indexes starting at any index I wish. Example starting at array[4] (g) would return
'g','a','b','c','c','d','e','f'
This is what I've tried so far
const notes = ['c','d','e','f','g','a','b','c']
var res = []
for (var i = 4; i < notes.length; i++) {
res.push(notes[i])
}
console.log(res)
You can use modulo % operator.
const getArray = (array, index) => {
const result = [];
const length = array.length;
for (let i = 0; i < length; i++) {
result.push(array[(index + i) % length]);
}
return result;
};
Simple way.
var notes = ['c','d','e','f','g','a','b','c'];
function looparr(arr, start)
{
var res = [], start = start || 0;
for(var index = start, length=arr.length; index<length; index++)
{
res.push(arr[index]);
index == (arr.length-1) && (index=-1,length=start);
}
return res;
}
console.log(looparr(['c','d','e','f','g','a','b','c'], 0));
console.log(looparr(['c','d','e','f','g','a','b','c'], 2));
console.log(looparr(['c','d','e','f','g','a','b','c'], 4));
console.log(looparr(['c','d','e','f','g','a','b','c']));
Very simple solution below :)
While i < index, remove the first character from the array, store it in a variable and then add it back onto the end of the array.
let array = ['c','d','e','f','g','a','b','c'];
var index = 4;
for(var i = 0; i < index; i++) {
var letter1 = array.shift(i); // Remove from the start of the array
array.push(letter1); // Add the value to the end of the array
}
console.log(array);
Enjoy :)

remove NaN in my array display

I want to ask help how to remove NaN value in my array. Every time I hit submit button without a data it will display a NaN value.
Variables for number sort :
var array = [];
var arrayasc = [];
var arraydsc = [];
function myFunction(){
var newArray = parseInt(document.getElementById("number").value);
array.push(newArray);
arrayasc.push(newArray);
arraydsc.push(newArray);
if
//entered numbers dislay here
document.getElementById("demo").innerHTML = array;
console.log(array);
//entered numbers in ascending order
mySort_asc(arrayasc);
document.getElementById("asc").innerHTML = array;
console.log(mySort_asc(arrayasc));
//entered numbers in descending order
mySort_dsc(arraydsc);
document.getElementById("dsc").innerHTML = array;
console.log(mySort_dsc(arraydsc));
}
Function to sort numbers in ascending order
function mySort_asc(arr){
for(var i = 0; i < array.length; i++) {
var temp = array[i];
var b = i - 1;
while (b >= 0 && array[b] > temp) {
array[b + 1] = array[b];
b--;
}
array[b + 1] = temp;
}
return array;
}
Function to sort numbers in descending order
function mySort_dsc(arr){
for(var i = 0; i < array.length; i++) {
var temp = array[i];
var b = i - 1;
while (b >= 0 && array[b] < temp) {
array[b + 1] = array[b];
b--;
}
array[b + 1] = temp ;
}
return array;
}
Your syntax is invalid
if
//entered numbers dislay here
document.getElementById("demo").innerHTML = array;
but simply check to see if the new value is a number or not, and if it isn't, return immediately without changing anything in the HTML:
function myFunction(){
var newArray = parseInt(document.getElementById("number").value);
if (Number.isNaN(newArray)) return;
You also might consider naming your variables to correspond to what they represent. For example, the newArray is actually just a single number, not an array. Perhaps call it newValue, for ease of reading and debugging?

Categories