0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

【PHPメモ】in_arrayで重複をチェック

Posted at

###0.目的
自分用のメモ

###1.重複文字問題
(1)112233が入力の時
⇒123
(2)233342213が入力の時
⇒2341
のように出力するプログラムを作る。

###2.メモ

in_array(検索対象文字列,配列):配列の中に検索対象文字列が存在するか確認する関数、存在する場合true,存在しない場合false

###3.コード

※paiza.IOで動作確認

<?php

    $in1 = fgets(STDIN);
    $in1=trim($in1);
    $len1=strlen($in1);
    //重複を取り除いた文字配列
    $arr_dict=[];
    
    for($i=0;$i<$len1;$i++){
        //$in1の1文字が$arr_dict配列に含まれない時
        if(!in_array(substr($in1,$i,1),$arr_dict)){
            $ans=substr($in1,$i,1);
            $arr_dict[]=$ans;
            echo $ans;
        } 
    }
    
?>

###入力

1234904743521627489064953371426326487539813121258593765158596901918

###出力

1234907568
0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?