发布网友 发布时间:2022-04-21 19:42
共2个回答
懂视网 时间:2022-05-15 02:50
这次给大家带来字符串+数组去重实战案例解析,字符串+数组去重的注意事项有哪些,下面就是实战案例,一起来看一下。
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title> js数组、字符串去重</title> </head> <body> <script type="text/javascript"> /*数组去重*/ function quchong(arr){ var len = arr.length; arr.sort(); for(var i=len-1;i>0;i--){ if(arr[i]==arr[i-1]){ arr.splice(i,1); } } return arr; } var a = ["a","a","b",'b','c','c','a','d']; var b = quchong(a); console.log(b); /*字符串去重*/ function quchongstr(str){ var a = str.match(/S+/g);//等价于str.split(/s+/g)// s空白符,S非空白符 a.sort(); for(var i=a.length-1;i>0;i--){ if(a[i]==a[i-1]){ a.splice(i,1); } } return a.join(" "); } var str = quchongstr("a a b a b e"); console.log(str); </script> </body> </html>
运行结果:
相信看了本文案例你已经掌握了方法,更多精彩请关注Gxl网其它相关文章!
推荐阅读:
JS怎样生成随机数
在实战项目中怎样使用jquery layur弹出层
热心网友 时间:2022-05-14 23:58
package lianXi;
public class helloWorld {
public static void main(String[] args) {
//初始化
String[][] rstop = { { "a", "b", "c", "d" }, { "b", "a", "b", "b" },
{ "e", "f", "g", "d" }, { "h", "i", "f", "k" } };
String[][] stop = new String[2][16];
for (int i = 0; i < stop.length; i++) {
for (int j = 0; j < stop[0].length; j++) {
stop[i][j] = "0";
}
}
int p = 0;
boolean boo = true;
//执行操作
for (int i = 0; i < rstop.length; i++) {
for (int j = 0; j < rstop[i].length; j++) {
for (int k = 0; k <= p; k++) {
if (stop[0][k].equals(rstop[i][j])) {
int temp = (Integer.parseInt(stop[1][k]) + 1);
stop[1][k] = temp + "";
boo = false;
break;
}
}
if (boo) {
stop[0][p] = rstop[i][j];
stop[1][p] = "1";
p++;
}
boo = true;
}
}
//输出结果
for (int i = 0; i < stop.length; i++) {
for (int j = 0; j < stop[0].length; j++) {
System.out.print(stop[i][j] + "\t");
}
System.out.println();
}
}
}
结果如下: