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. 解决电脑开机连不上网问题(Windows检测:远程计算机或设备将不接受连接)

    打开Google Chrome浏览器  -----> 设置 ------> 高级设置 -----> 打开代理设置 -----> 连接 -----> 局域网设置 ----& ...

  2. Evaluating Automatically Generated timelines from the Web (paper1)

    摘要: 问题:There is a need that 以一个更全面/更综合的方式来展现搜索结果.对此,作者正在开发一个系统,called “Cronopath”,这个系统将产生一个时间线,通过决定每 ...

  3. Mac配置虚拟环境Virtualenv,安装Python科学计算包详解

    参考: https://www.jianshu.com/p/51140800e8b4 安装 virtualenvwrapper Virtaulenvwrapper是virtualenv的扩展包,可以更 ...

  4. Oracle 计算时间格式平均值

    select to_char((to_date('2019-07-01', 'yyyy-mm-dd') + numtodsinterval(avg(begin_time_second), 'secon ...

  5. Java 包的理解与使用

    java中的包可以分为两种:内置包.用户自己定义的包.这里介绍的是用户自定义的包. 一.包的使用 1.创建PackageTest.java package com.packtest; public c ...

  6. Excel时间序列函数

    year 返回对应于某个日期的年份. month 返回对应于某个日期的月份. day 返回对应于某个日期的年份. weekday 返回对应于某个日期的天数. weeknum 返回对应日期在本年中是第几 ...

  7. 学习Electorn(1)——Hello World

    环境 操作系统是Manjaro kde 18.01 按照官网文档:https://electronjs.org/docs 安装node https://nodejs.org/en/download/p ...

  8. Idea中一个服务按多个端口同时启动

    1.勾选并行启动 2.-Dserver.port=9018

  9. Jfinal初次尝试及Jetty使用相关问题

    Jetty介绍 Jetty官网:http://www.eclipse.org/jetty/ 参考:Jetty使用教程(一)--开始使用Jetty 1. 使用 参考上面文档,但是有些更新: echo $ ...

  10. CAS 5.x搭建常见问题系列(2).PKIX path building failed

    错误原因 服务端的证书是不安全的,Cas的客户端在调用时因为安全提醒造成调用失败. CAS的客户端需要导入服务端的证书后,就正常了. 具体操作步骤如下: 1. 首先启动tomcat,看下之前搭建的ca ...