golang 2 ways to delete an element from a slice
2 ways to delete an element from a slice

Fast version (changes order)
a := []string{"A", "B", "C", "D", "E"}
i := 2
// Remove the element at index i from a.
a[i] = a[len(a)-1] // Copy last element to index i.
a[len(a)-1] = "" // Erase last element (write zero value).
a = a[:len(a)-1] // Truncate slice.
fmt.Println(a) // [A B E D]
The code copies a single element and runs in constant time.
Slow version (maintains order)
a := []string{"A", "B", "C", "D", "E"}
i := 2
// Remove the element at index i from a.
copy(a[i:], a[i+1:]) // Shift a[i+1:] left one index.
a[len(a)-1] = "" // Erase last element (write zero value).
a = a[:len(a)-1] // Truncate slice.
fmt.Println(a) // [A B D E]
The code copies len(a) - i - 1 elements and runs in linear time.
golang 2 ways to delete an element from a slice的更多相关文章
- Why Go? – Key advantages you may have overlooked
Why Go? – Key advantages you may have overlooked yourbasic.org/golang Go makes it easier (than Java ...
- Golang 学习资料
资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...
- golang(10)interface应用和复习
原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...
- Save Update saveOrUpdate delete
参考:Hibernate Session的saveOrUpdate()方法 saveOrUpdate与Update的作用 Hibernate Delete query Hibernate Basics ...
- [Swift]LeetCode740. 删除与获得点数 | Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- 说说不知道的Golang中参数传递
本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...
- 740. Delete and Earn
Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...
- [LeetCode]Delete and Earn题解(动态规划)
Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...
- leetcode笔记(六)740. Delete and Earn
题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...
随机推荐
- Vue项目过程中遇到的小问题
1.给router-link添加点击事件 <router-link to="" @click.native=""></router-link& ...
- 【转载】49个CSS知识点
01.[负边距]
- (十一)El表达式详细介绍
看之前,最好先看下 el表达式快速入门 本来将重点讲下 el表达式 能干嘛 : 目录 执行计算 获得 web 开发常用对象 关于 param 与 paramValues 的用法: 关于 header ...
- mysql 行(记录)的详细操作
阅读目录 一 介绍 二 插入数据INSERT 三 更新数据UPDATE 四 删除数据DELETE 五 查询数据SELECT 六 权限管理 一 介绍 MySQL数据操作: DML =========== ...
- Itellij IDEA下Maven的配置
maven基本配置 配置阿里云镜像 打开settings.xml,添加 <mirrors> <mirror> <id>alimaven</id> < ...
- SAS学习笔记34 指针控制
指针控制符分为行指针和列指针两种 列指针控制符模式 @n:指明列的开始位置,是对应变量的数据开始列位置 列控制符号模式 n1-n2:n1列开始位置,n2列结束位置 @与@@符号应用 @行控制符号,控制 ...
- Codeforces 1245 D. Shichikuji and Power Grid
传送门 经典的最小生成树模型 建一个点 $0$ ,向所有其他点 $x$ 连一条边权为 $c[x]$ 的边,其他任意两点之间连边,边权为 $(k_i+k_j)(\left | x_i-x_j\right ...
- hdu 4857 反向拓扑问题
尤其要注意拓扑的分层问题 不难理解 就是不怎么好想到 拓扑的思路这里就不累述了 #include <iostream> #include <cstdio> #include & ...
- (二)Struts.xml文件详解
一.Struts.xml文件 Struts.xml文件构成 如图,<Struts>标签内共有5个子标签. 1.1 struts-default.xml 查看Struts的内容可知,Stru ...
- DSO missing from command line
最近项目正经历着一次更新,一系列编译工具都进行了大版本的升级,随时而来的是,原本正常编译的代码出现了大量的warning,最终编译失败,其 中一个问题困扰了比较长的时间,虽然fix的方法不难,但是一直 ...