Skip to content

Instantly share code, notes, and snippets.

@shettayyy
Last active May 1, 2020 11:29
Show Gist options
  • Save shettayyy/d9f6a7c5d14c2202b60a657cbfadeaa8 to your computer and use it in GitHub Desktop.
Save shettayyy/d9f6a7c5d14c2202b60a657cbfadeaa8 to your computer and use it in GitHub Desktop.
Naive solution to find the value present in one array squared in another array https://jsbin.com/muxaxil
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
</head>
<body>
<script id="jsbin-javascript">
// Naive solution to find the value present in one array squared in another array
function same1(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
for(let i = 0; i < arr1.length; i++){
let correctIndex = arr2.indexOf(arr1[i] ** 2);
if(correctIndex === -1) {
return false;
}
console.log(arr2);
arr2.splice(correctIndex,1)
}
return true;
}
same1([1,2,3,2], [9,1,4,4])
// Naive solution to find the value present in one array squared in another array
function same2(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
let frequencyCounter1 = {}
let frequencyCounter2 = {}
for(let val of arr1){
frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1
}
for(let val of arr2){
frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1
}
console.log(frequencyCounter1);
console.log(frequencyCounter2);
for(let key in frequencyCounter1){
if(!(key ** 2 in frequencyCounter2)){
return false
}
if(frequencyCounter2[key ** 2] !== frequencyCounter1[key]) {
return false
}
}
return true
}
same2([1,2,3,2,5], [9,1,4,4,11])
</script>
<script id="jsbin-source-javascript" type="text/javascript">// Naive solution to find the value present in one array squared in another array
function same1(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
for(let i = 0; i < arr1.length; i++){
let correctIndex = arr2.indexOf(arr1[i] ** 2);
if(correctIndex === -1) {
return false;
}
console.log(arr2);
arr2.splice(correctIndex,1)
}
return true;
}
same1([1,2,3,2], [9,1,4,4])
// Naive solution to find the value present in one array squared in another array
function same2(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
let frequencyCounter1 = {}
let frequencyCounter2 = {}
for(let val of arr1){
frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1
}
for(let val of arr2){
frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1
}
console.log(frequencyCounter1);
console.log(frequencyCounter2);
for(let key in frequencyCounter1){
if(!(key ** 2 in frequencyCounter2)){
return false
}
if(frequencyCounter2[key ** 2] !== frequencyCounter1[key]) {
return false
}
}
return true
}
same2([1,2,3,2,5], [9,1,4,4,11])
</script></body>
</html>
// Naive solution to find the value present in one array squared in another array
function same1(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
for(let i = 0; i < arr1.length; i++){
let correctIndex = arr2.indexOf(arr1[i] ** 2);
if(correctIndex === -1) {
return false;
}
console.log(arr2);
arr2.splice(correctIndex,1)
}
return true;
}
same1([1,2,3,2], [9,1,4,4])
// Naive solution to find the value present in one array squared in another array
function same2(arr1, arr2){
if(arr1.length !== arr2.length){
return false;
}
let frequencyCounter1 = {}
let frequencyCounter2 = {}
for(let val of arr1){
frequencyCounter1[val] = (frequencyCounter1[val] || 0) + 1
}
for(let val of arr2){
frequencyCounter2[val] = (frequencyCounter2[val] || 0) + 1
}
console.log(frequencyCounter1);
console.log(frequencyCounter2);
for(let key in frequencyCounter1){
if(!(key ** 2 in frequencyCounter2)){
return false
}
if(frequencyCounter2[key ** 2] !== frequencyCounter1[key]) {
return false
}
}
return true
}
same2([1,2,3,2,5], [9,1,4,4,11])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment