2 ways to delete an element from a slice

yourbasic.org/golang

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的更多相关文章

  1. 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 ...

  2. Golang 学习资料

    资料 1.How to Write Go Code https://golang.org/doc/code.html 2.A Tour of Go https://tour.golang.org/li ...

  3. golang(10)interface应用和复习

    原文链接 http://www.limerence2017.com/2019/10/11/golang15/ interface 意义? golang 为什么要创造interface这种机制呢?我个人 ...

  4. Save Update saveOrUpdate delete

    参考:Hibernate Session的saveOrUpdate()方法 saveOrUpdate与Update的作用 Hibernate Delete query Hibernate Basics ...

  5. [Swift]LeetCode740. 删除与获得点数 | Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  6. 说说不知道的Golang中参数传递

    本文由云+社区发表 导言 几乎每一个C++开发人员,都被面试过有关于函数参数是值传递还是引用传递的问题,其实不止于C++,任何一个语言中,我们都需要关心函数在参数传递时的行为.在golang中存在着m ...

  7. 740. Delete and Earn

    Given an array nums of integers, you can perform operations on the array. In each operation, you pic ...

  8. [LeetCode]Delete and Earn题解(动态规划)

    Delete and Earn Given an array nums of integers, you can perform operations on the array. In each op ...

  9. leetcode笔记(六)740. Delete and Earn

    题目描述 Given an array nums of integers, you can perform operations on the array. In each operation, yo ...

随机推荐

  1. 【miscellaneous】理解Gstreamer架构

    本文给出了Gstreamer的总体设计.通过阅读本文可以了解Gstreamer的内部工作原理.本文编译自gstreamer源码中的文档,原文在源码中的位置是/gstreamer/docs/design ...

  2. windows下进程与线程

    windows下进程与线程 Windows是一个单用户多任务的操作系统,同一时间可有多个进程在执行.进程是应用程序的运行实例,可以理解为应用程序的一次动态执行:而线程是CPU调度的单位,是进程的一个执 ...

  3. 最新 美柚java校招面经 (含整理过的面试题大全)

    从6月到10月,经过4个月努力和坚持,自己有幸拿到了网易雷火.京东.去哪儿. 美柚等10家互联网公司的校招Offer,因为某些自身原因最终选择了 美柚.6.7月主要是做系统复习.项目复盘.LeetCo ...

  4. JS选中和取消选中checkbox

    document.getElementsByTagName("input")[0].checked = 0;//不选择 document.getElementsByTagName( ...

  5. VBA实现打开Excel文件读取内容拷贝Format且加超链接

    '-------------------一覧取得----------------------------- Sub getRedmineGrid_Click() Dim wb As Workbook ...

  6. layui的日期控件使用

    导包请看官方:https://www.layui.com/doc/modules/laydate.html#use 例如: <input type="text" id=&qu ...

  7. Windows用Eclipse来开发hadoop的WordCount的helloworld

    [学习笔记] 2.Win7用Eclipse来开发hadoop的WordCount的helloworld网上下载hadoop-eclipse-plugin-2.7.4.jar,将该jar包拷贝到Ecli ...

  8. python标准库之collections介绍

    collections----容器数据类型 collections模块包含了除list.dict.和tuple之外的容器数据类型,如counter.defaultdict.deque.namedtup ...

  9. 浅析ARM协处理器CP15寄存器有关指令:MCR\MRC

    ref:http://blog.csdn.net/gameit/article/details/13169405 背景: 在uboot中,start.s中涉及到了 CP15 的有关操作.查阅有关资料, ...

  10. Elasticsearch 介绍及应用

    Elasticsearch简单介绍 Elasticsearch (ES)是一个基于Lucene构建的开源.分布式.RESTful 接口全文搜索引擎.Elasticsearch 还是一个分布式文档数据库 ...