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. sql中能使用charindex 不要用 in 。charindex比in快很多

    写SQL语句我们经常需要判断一个字符串中是否包含另一个字符串,但是SQL SERVER中并没有像C#提供了Contains函数,不过SQL SERVER中提供了一个叫CHAEINDX的函数,顾名思义就 ...

  2. 第二坑:Linux发布项目

    在这里踩过两个坑: 1.第一个是上传文件的时候,不知道是什么原因,上传失败了,然后发布新版本的时候,依然用的上个版本的包,导致工作出现重大失误. 谨记:上传文件的时候,检查一下上传文件和本地文件的时间 ...

  3. 验证码处理+cookie模拟登录

    一.背景 相关博文:https://www.jianshu.com/p/9fce799edf1e https://blog.csdn.net/h19910518/article/details/793 ...

  4. 1263: 你会做蛋糕吗?(Java)

    WUSTOJ 1263: 你会做蛋糕吗? 参考博客 Mitsuha_的博客 Description BobLee是个大吃货,喜欢吃好吃的,也喜欢做好吃的.比如做正方形的蛋糕.比如下图这个5*5的蛋糕. ...

  5. scratch少儿编程第一季——02、scratch界面介绍

    各位小伙伴大家好: 上期我们简单的介绍了Scratch的一些基本信息,和scratch软件的下载. 今天我们一起来了解一下Scratch的编程界面的介绍. 关于版本我考虑之后还是决定基于Scratch ...

  6. 怎样理解构造函数的原型对象prototype

    通过构造函数生成的实例对象中的属性和方法其实是从构造函数中"copy"一份后生成的, 也就是说虽然生成的对象是构造函数的实例, 但里面的属性和方法确实相互独立的, 比如下面的lil ...

  7. css中字体常用单位px、em、rem和%的区别及用法总结

    一.px.em.rem和%的定义 1.px(像素) px单位的名称为像素,它是一个固定大小的单元,像素的计算是针对(电脑/手机)屏幕的,一个像素(1px)就是(电脑/手机)屏幕上的一个点,即屏幕分辨率 ...

  8. (八)easyUI之Accordion折叠面板:动态面板

    二.动态面板 数据库设计 函数设计:该函数用于获取某个节点的所有子节点 CREATE FUNCTION fn_getAddress_ChildList_test(rootId INT) RETURNS ...

  9. Quartz任务调度:MisFire策略和源码分析

    Quartz是为大家熟知的任务调度框架,先看看官网的介绍: ---------------------------------------------------------------------- ...

  10. 关于微信小程序使用watch监听数据变化的方法

    众所周知,Vue中,可以使用监听属性 watch来观察和响应 Vue 实例上的数据变化,那么小程序能不能实现这一点呢? 监听器的原理,是将data中需监听的数据写在watch对象中,并给其提供一个方法 ...