当前位置:网站首页>DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
DAY_ 4. Operation -- judge whether there is a certain data in the array -- realize array mapping (enlarge by 10 times) -- insert the array in sequence (modify bugs) -- realize array de duplication
2022-07-27 21:28:00 【Brave * Niuniu】
Packaging function , Determine whether there is a certain data in the array
var arr = [7,3,24,5,20,1,0,12,10,0,5]
function hasData(arr, data) {
var res = arr.indexOf(data);
return res !=-1 ? true : false;
}
Packaging function , Implement array mapping ( Zoom in 10 times )
function mapArr(arr) {
for(var i=0;i<arr.length;i++){
arr[i] = arr[i]*10;
console.log(arr[i]);
}
}
Algorithm implementation :
Packaging function , Insert array in order
// Judge whether it is ascending or descending
function fun(arr){
var i =0
while(true){
if(arr[i] != arr[i+1]){
return arr[i]<arr[i+1] ? true:false
}
i++;
}
}
function insert(arr, data) {
if(fun(arr)){
if(data>=arr[arr.length-1]){
arr.push(data)
return 0
}
if(data<=arr[0]){
arr.unshift(data)
return 0
}
for(var i=0; i< arr.length; i++){
if(data >= arr[i] && data <= arr[i+1]){
console.log(arr[i],arr[i+1]);
arr.splice(i+1,0,data)
break;
}
}
}else{
if(data<=arr[arr.length-1]){
arr.push(data)
return 0
}
if(data>=arr[0]){
arr.unshift(data)
return 0
}
for(var i=0; i< arr.length; i++){
if(data <= arr[i] && data >= arr[i+1]){
// console.log(arr[i],arr[i+1]);
arr.splice(i+1,0,data)
break;
}
}
}
}
//var arr = [1,4,8,9,12,12]
var arr = [12,12,8,6,2,1]
insert(arr,5)
console.log(arr);
console.log(fun(arr));
=> Tips :
-> The inserted data may be the same as the internal data of the array
-> Keep the original sorting method
Implementation of array de duplication
=> explain : Consider only arrays of pure numbers
=> Tips : When you delete an item in the array , The index will collapse later
There is indeed a big hole in this one , Cautious brothers and sisters !!!!!!( I'm late for dinner )
Algorithm implementation :
var arr = [3,2,10,3,1,2,10,8,9,1,10,6,10];
console.log(arr);
//var arr = [8,1,8,8,90,90,8];
// var arr = [3,1,10,10,3,1,2];
var arr = [3,1,3,3,1,4]
console.log(arr);
var size = arr.length;
var count = 0;
for(var i=0; i<arr.length;i++){
for(var j=i+1;j<arr.length;j++){
if(arr[i] == arr[j]){
console.log(` The current subscript of the comparison is :${
i}`);
console.log(` Duplicate item :${
arr[j]}, Index subscript is ${
j}`);
arr.splice(j,1)
console.log("=> After the current weight removal :",arr);
j--;
}
console.log(`<=======j yes :${
j}=========>`);
}
}
console.log(arr);
边栏推荐
- Report design - how to make your powerbi Kanban brilliant?
- Unity 安装个人免费版
- Chinese and English instructions - human alpha fetoprotein (AFP) ELISA quantitative Kit
- Knife4j dynamically refreshes global parameters through JS
- Implicit intent
- Characteristics of exonuclease in Worthington venom and related literature
- zibbix安装部署
- 行为级描述与RTL级描述
- What are the product performances of industrial Ethernet switches?
- Some operations about Anaconda (installing software and quickly opening)
猜你喜欢

Knife4j dynamically refreshes global parameters through JS

A new technical director asked me to do an IP territorial function~

Worthington毒液中核酸外切酶的特征及相关文献

JVM-内存模型 面试总结

Dobot magician robot arm - Introduction

ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器

Pytest失败重跑

mysql 最大建议行数2000w,靠谱吗?

基于DSP 回传音通话降噪链路设计

PostgreSQL source code (65) analysis of the working principle of globalvis, a new snapshot system
随机推荐
Big guys, the MySQL version is low and does not support CDC, so canal synchronizes binlog to Kafka and data to cli
The new CTO strongly prohibits the use of calendar?
Win11 user name and password backup method
The use experience of the product is up to you in the evaluation and exchange meeting of the flying oar frame experience!
Worthington phospholipase A2 study phosphatidylcholine 2-acetylhydrolase
ECCV 2022 | 中科大&京东提出:数据高效的Transformer目标检测器
What if the start button doesn't respond after the win11 system updates kb5014668?
飞桨框架体验评测交流会,产品的使用体验由你来决定!
crsctl中,显示的HOME的作用
R language uses dplyr package to connect two dataframe data (left join)
The solution that the laptop can connect to WiFi but the browser cannot open the web page
The maximum recommended number of rows for MySQL is 2000W. Is it reliable?
“地理-语言”大模型文心ERNIE-GeoL及应用
Worthington磷脂酶A2研究丨磷脂酰胆碱2-乙酰水解酶
MySQL data recovery process is based on binlog redolog undo
Common ArrayList interview questions
Puzzle (021) eliminate problems
Thinking about SLA of cloud computing
中国能否在元宇宙的未来发展中取得突破,占领高地?
进程管理 进程的监控与管理 ps/pstree/top/lsof