8
\$\begingroup\$

Challenge:

Given the input array l with a list of strings, only keep the elements in the sequence that have a letter that's repeated at least 3 times. Like 'lessons' has 3 s letters, so it should be kept in the sequence. But, 'lesson' has only two s letters, so it should be removed.

Notes:

  • l will always be a sequence, and its elements will always be strings with only alphabetical characters.

  • I am using the example output with Python Lists. You can use any type of sequence in your own language.

Test cases:

['element', 'photoshop', 'good'] -> ['element', 'photoshop']
['happy', 'colorful', 'luggage'] -> ['luggage']
['reference', 'tomorrow', 'today'] -> ['reference', 'tomorrow'] 

This is , so the shortest code in bytes wins!

\$\endgroup\$
6
  • 4
    \$\begingroup\$ This is fine for now, but in future, please note that it's recommended to use the Sandbox before you post your challenge, so you can get feedback on it first. \$\endgroup\$
    – The Thonnu
    Dec 22, 2022 at 12:26
  • 1
    \$\begingroup\$ I will @TheThonnu next time. \$\endgroup\$ Dec 22, 2022 at 12:27
  • \$\begingroup\$ @U12-Forward order doesn't matter right? \$\endgroup\$ Dec 23, 2022 at 12:50
  • 2
    \$\begingroup\$ Can the input array have repeates? \$\endgroup\$
    – xnor
    Dec 23, 2022 at 22:55
  • \$\begingroup\$ @UndoneStudios It doesn't matter. \$\endgroup\$ Dec 25, 2022 at 9:07

33 Answers 33

10
\$\begingroup\$

Python 3, 51 50 bytes

lambda l:{s for s in l for c in s if s.count(c)>2}

Try it online!

\$\endgroup\$
9
  • 1
    \$\begingroup\$ Nice, I love the set unpack part. \$\endgroup\$ Dec 22, 2022 at 12:23
  • 1
    \$\begingroup\$ @U12-Forward thanks, I learnt it from here \$\endgroup\$
    – The Thonnu
    Dec 22, 2022 at 12:24
  • 1
    \$\begingroup\$ This will output a string multiple times if more than one of its letters appears three times. \$\endgroup\$
    – xnor
    Dec 23, 2022 at 0:04
  • 1
    \$\begingroup\$ @xnor fixed (by just changing the list comprehension to a set comprehension) \$\endgroup\$
    – The Thonnu
    Dec 23, 2022 at 9:18
  • 1
    \$\begingroup\$ This is probably the most pythonic code golf answer I've seen in a while \$\endgroup\$
    – jezza_99
    Dec 23, 2022 at 22:11
6
\$\begingroup\$

QuadS, 13 bytes

Takes linebreak-separated text

(.).*\1.*\1
%

Try it online!

Search for…

(.) any character (1)
.* zero or more characters
\1 character (1)
.* zero or more characters

and for each match, return:

% the entire line the match occurred on

\$\endgroup\$
6
\$\begingroup\$

Japt -f, 7 4 bytes

ü d¤

Try it

ü d¤     :Implicit filter of input array
ü        :Group & sort
  d      :Any truthy (a non-empty string)
   ¤     :  Slice off the first 2 characters

Without flag, 6 bytes

fÈü d¤

Try it

\$\endgroup\$
6
\$\begingroup\$

Python 3, 55 bytes

lambda a:filter(lambda x:any(x.count(i)>2for i in x),a)

Returns a filter object. The asterisk is used in the output to unpack the filter object automatically.

Try it online!

Python 2, 55 bytes

lambda a:filter(lambda x:any(x.count(i)>2for i in x),a)

Exactly the same code, just returns a normal Python list instead of a filter object, meaning you don't need to use the asterisk in the output (if that matters).

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Nice first answer! \$\endgroup\$ Dec 23, 2022 at 2:28
6
\$\begingroup\$

Nibbles, 6.5 5.5 bytes (11 nibbles)

Edit: -2 bytes by using transpose, inspired by Jonathan Allan's Jelly answer

|$>2`'=~$$
|$              # filter each word int the input by
      =~$       #   group the letters by
         $      #     themselves,
    `'          #   transpose this list of lists,
  >2            #   and remove the first 2 elements
                #   (so if there is at least one letter
                #   present >2 times, the resulting 
                #   list will have nonzero length)

enter image description here

\$\endgroup\$
5
\$\begingroup\$

GNU grep -P, 11 bytes

(.).*\1.*\1

P.a. to selecting the language flavour for perl, I'm not counting -P (which sets the language flavour to PCRE from the default BRE).

"List of string" defined as "text file" (POSIX).

Test case typescript:

$ printf '%s\n' \
  'element' 'photoshop' 'good' \
  'happy' 'colorful' 'luggage' \
  'reference' 'tomorrow' 'today' \
| grep -P '(.).*\1.*\1'
element
photoshop
luggage
reference
tomorrow
\$\endgroup\$
1
  • \$\begingroup\$ Feel free to upvote my question :) \$\endgroup\$ Dec 22, 2022 at 15:33
5
\$\begingroup\$

Python 3, 48 bytes

lambda s:[c for c in s if max(map(c.count,c))>2]

Try it online!

\$\endgroup\$
6
  • \$\begingroup\$ Nice! Unfortunately, I won't upvote it, because of the misleading variable names (s instead of L, c instead of s). \$\endgroup\$
    – Niccolo M.
    Dec 25, 2022 at 6:44
  • 1
    \$\begingroup\$ @NiccoloM. s&c for set of candidates. :) \$\endgroup\$
    – tsh
    Dec 25, 2022 at 10:46
  • \$\begingroup\$ That's a good excuse. Upvoted! \$\endgroup\$
    – Niccolo M.
    Dec 25, 2022 at 12:58
  • \$\begingroup\$ @NiccoloM. this is codegolf, generally no one cares what the variable names are because they are all arbitrary letters anyway \$\endgroup\$
    – jezza_99
    Jan 5, 2023 at 23:35
  • \$\begingroup\$ @jezza_99 And, tbh, I don't even type capital letters to begin with. u can see me not use them in the beginning of sentences. i don't capitalize the word "I", because that's just unnecessary english jargon. i don't type them at the beginning of names. i just type them if i absolutely have to. \$\endgroup\$
    – Joao-3
    Jan 6, 2023 at 12:07
4
\$\begingroup\$

Ruby, 25 bytes

->l{l.grep /(.).*\1.*\1/}

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ You can knock out a couple bytes by making it a full program. \$\endgroup\$
    – Jordan
    Dec 22, 2022 at 16:59
  • 1
    \$\begingroup\$ 21 bytes using Jordan's idea. \$\endgroup\$
    – south
    Dec 23, 2022 at 21:32
4
\$\begingroup\$

Pip -p, 11 bytes

gFI2<M:^_N_

Try It Online!

Explanation

gFI2<M:^_N_
g            List of command-line arguments
 FI          Filter by this function:
   2<          2 is less than
     M         the maximum of
      :        (force the precedence of unary M to be lower than its rhs)
         N_    the number of occurrences in the argument string of
       ^_      each character in the argument string
\$\endgroup\$
3
\$\begingroup\$

JavaScript, 37 bytes

I/O as an array of strings, using GB's Adám's RegEx.

a=>a.filter(s=>/(.).*\1.*\1/.test(s))

Try it online!

44 42 bytes

I/O as an array of character arrays.

a=>a.filter(a=>a.some(c=>(a[c]=-~a[c])>2))

Try it online!

\$\endgroup\$
0
3
\$\begingroup\$

Vyxal, 7 6 bytes

'sĠ'ḢḢ

Try it Online!

Explained

'sĠ'ḢḢ
'      # Filter input where:
 sĠ    #   sorted and grouped by consecutive
   'ḢḢ #   has items where length >= 3
\$\endgroup\$
0
3
\$\begingroup\$

Retina 0.8.2, 13 bytes

G`(.).*\1.*\1

Try it online! Takes newline-separated words. Explanation: G is Retina's "grep" operation, which just keeps matching lines, according to the obvious regular expression.

\$\endgroup\$
3
\$\begingroup\$

sed -En, 14 bytes

/(.).*\1.*\1/p

Attempt This Online!

\$\endgroup\$
3
\$\begingroup\$

x86 .COM, 48 bytes

0100  BA 2F 01 B8 03 0A 89 C7-89 C1 F3 AA CD 21 BE 31
0110  01 8A 4C FF 0F B6 D9 C7-40 01 0A 24 AC 89 C3 FE
0120  0F 74 04 E2 F7 EB D9 B2-31 B4 09 CD 21 EB D1 FF

org 100h
sta:    mov dx, bu
    mov ax, 0x0A03
    mov di, ax
    mov cx, ax
la: rep stosb
    int 21h
    mov si, bu+2
    mov cl, [si-1]
    movzx bx, cl
    mov [bx+si+1], word '$'*256+10
lb: lodsb
    mov bx,ax
    dec byte [bx]
    jz ok
    loop lb
    jmp sta
ok: mov dl, bu+2-$100
    mov ah, 9
    int 21h
    jmp sta
bu: db -1

Unluckily it behaves bad, input is also displayed

\$\endgroup\$
3
\$\begingroup\$

Jelly, 6 bytes

ĠZṫɗƇ3

A monadic Link that accepts a list of lists of characters and yields the filtered list.

Try it online!

How?

ĠZṫɗƇ3 - Link: words
     3 - set the right argument to three
    Ƈ  - filter (words) keeping those for which:
   ɗ   -   last three links as a dyad - f(word, 3):
Ġ      -     group indices by value - e.g. "aardvark" -> [[1,2,6],[4],[8],[3,7],[5]]
 Z     -     transpose                                   [[1,4,8,3,5],[2,7],[6]]
  ṫ    -     tail from index (3)                         [[6]]
       -     (an empty list is falsey)                   true (the third "a" gives the 6)
\$\endgroup\$
3
+150
\$\begingroup\$

Pip -p, 14 bytes

{FIaM_Na>2}FIg

Try It Online!

{FIaM_Na>2}FIg
{         }FIg   Keep words that return truthy resuts for ...
   aM_Na>2       Does the letter appear more than two times?
 FI              Filter out falsy results (return either "1" or "")
\$\endgroup\$
2
  • \$\begingroup\$ FIg \$\endgroup\$
    – Seggan
    Jan 19, 2023 at 15:05
  • \$\begingroup\$ @Seggan Fig yay \$\endgroup\$
    – math scat
    Jan 19, 2023 at 15:08
2
\$\begingroup\$

Charcoal, 12 bytes

WS¿⊙ι›№ικ²⟦ι

Try it online! Link is to verbose version of code. Takes newline-terminated words. Explanation:

WS

Loop through the words.

¿⊙ι›№ικ²

If any character appears more than twice, then...

⟦ι

... output the word on its own line.

9 bytes by taking the input in JSON format:

Φθ⊙ι›№ιλ²

Try it online! Link is to verbose version of code. Explanation:

 θ          Input array
Φ           Filtered where
   ι        Current word
  ⊙         Any character satisfies
     №      Count of
       λ    Current character
      ι     In current word
    ›       Is greater than
        ²   Literal integer `2`
            Implicitly output each match on its own line
\$\endgroup\$
2
\$\begingroup\$

Jelly, 7 bytes

ĠẈ>2ẸµƇ

Try it online!

How it works

ĠẈ>2ẸµƇ - Main link. Takes a list of words L on the left
     µƇ - Filter L by the following:
Ġ       -   Group the indices of identical letters
 Ẉ      -   Length of each group
  >2    -   Greater than 2?
    Ẹ   -   Any true?
\$\endgroup\$
2
\$\begingroup\$

C (gcc), 95 90 85 bytes

  • -5 thanks to l4m2
  • -5 thanks to ceilingcat and c--

Lowercase strings only.

Iterates through each string and prints it as soon as it encounters 3 of a letter, skipping the string otherwise. Rather than clear the counters each time, I simply create brand new counters.

f(int**s){for(char*t,*i;t=*s;*t&&puts(*s),s++)for(i=calloc(99,9);*t&&++i[*t]<3;t++);}

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ 92 \$\endgroup\$
    – l4m2
    Dec 23, 2022 at 10:28
  • \$\begingroup\$ Also your j isn't used \$\endgroup\$
    – l4m2
    Dec 23, 2022 at 10:30
  • \$\begingroup\$ 81 bytes and it doesn't leak, not that it matters \$\endgroup\$
    – c--
    Feb 13, 2023 at 16:46
2
\$\begingroup\$

Java 8, 100 bytes

l->{l.removeIf(s->s.chars().noneMatch(c->s.length()-s.replace(""+(char)c,"").length()>2));return l;}

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Python 3, 541 53 50 bytes

To get the ball rolling...

-1 thanks to a fix pointed out by xnor
-3 bytes because sets are also fine

lambda n:{i for i in n for y in i if i.count(y)>2}

1: crossed out 54 still looks like 54 :(

\$\endgroup\$
5
  • \$\begingroup\$ Nice answer +1. \$\endgroup\$ Dec 22, 2022 at 12:10
  • 2
    \$\begingroup\$ Note that your challenges are getting easier and easier. I suggest next time you try increasing the difficulty of your challenges. \$\endgroup\$ Dec 22, 2022 at 12:11
  • \$\begingroup\$ Sure, I will take that to note. \$\endgroup\$ Dec 22, 2022 at 12:12
  • 1
    \$\begingroup\$ This wasn't hard though, so it was kinda fun trying to get a solution before I realised there was already an answer up there. \$\endgroup\$ Dec 22, 2022 at 12:14
  • \$\begingroup\$ This will output a string multiple times if more than one of its letters appears three times. \$\endgroup\$
    – xnor
    Dec 23, 2022 at 0:04
1
\$\begingroup\$

APL (Dyalog Extended), 11 bytes

Anonymous tacit prefix function taking a list of strings

⊢/⍨3∊¨3⌊⍧⍨¨

Try it online!

 the argument…

/⍨ filtered by…

3∊¨ whether 3 is a member of each,…

3⌊ where 3 caps the values of…

⍧⍨¨ the self-counts for each

\$\endgroup\$
1
\$\begingroup\$

Pyth, 7 bytes

.-#+GGQ

Try it online!

Explanation

  #        # filter each element of
      Q    # eval(input())
.-         # on the bag-wise subtraction of
   +GG     # two alphabets concatenated
\$\endgroup\$
1
\$\begingroup\$

Factor + math.unicode, 43 bytes

[ [ histogram values [ 2 > ] ∃ ] filter ]

Attempt This Online!

  • [ ... ] filter Select sequences whose
  • histogram histograms
  • values [ ... ] ∃ contain any values
  • 2 > greater than two
\$\endgroup\$
1
\$\begingroup\$

Haskell, 54 bytes

import Data.List
r=filter(any((>2).length).group.sort)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 7 6 bytes

ʒD¢à3@

I/O as a list of lists of characters.

Try it online.

Old 7 bytes answers with I/O as a list of strings:

ʒ{Åγà3@

Try it online.

{Åγà could alternatively be D.M¢ for the same byte-count:
Try it online.

Explanation:

ʒ       # Filter the (implicit) input-list of lists of characters:
 D      #  Duplicate the current list of characters
  ¢     #  Pop both, and count how many times each character occurs in the list
   à    #  Pop and leave the maximum
    3@  #  Check whether this maximum is >= 3
        # (after which the filtered list of lists of characters is output implicitly)

ʒ       # Filter the (implicit) input-list of strings:
 {      #  Sort the characters in the string
  Åγ    #  Run-length encode it; pushing a list of characters and lengths separated to
        #  the stack
    à   #  Pop the list of lengths and leave its maximum
     3@ #  Check whether this maximum is >= 3
        # (after which the filtered list of strings is output implicitly)

 D      #  Duplicate the current string
  .M    #  Pop and push its most frequently occurring character
    ¢   #  Pop both, and count how many times this most frequent character occurs in the
        #  string
\$\endgroup\$
1
\$\begingroup\$

QB45, 122 bytes

for t=1to n:redim p(26):for i=1to len(a$(t)):v=asc(mid$(a$(t),i,1))-96:p(v)=p(v)+1:if p(v)>2then ?a$(t):exit for
next:next
\$\endgroup\$
1
\$\begingroup\$

PowerShell Core, 30 bytes

$args|?{$_-match'(.).*\1.*\1'}

Try it online!

Or 40 bytes without a regex: $args|?{($_|% t*y|group|%{$_|% c*})-ge3}

\$\endgroup\$
1
\$\begingroup\$

Arturo, 62 35 bytes

$=>[select&=>[tally&|some?=>[&>2]]]

Try it

-27 due to 0.9.82 introducing tally.

\$\endgroup\$
1
\$\begingroup\$

Julia 1.7, 37 bytes

!l=l[l.|>i->any(count.([i...],i).>2)]

Try it online!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.