find duplicate characters count in a string

input: "Banana",

output: "B---------------1" , "aaa---------------3", "nn---------------2" 

any one try it anthor way ..

{

a!localVariables(
local!data: "Banana",
local!len: len(local!data),
local!merge: union(
a!forEach(
items: enumerate(local!len) + 1,
expression: local!data[fv!index]
),
a!forEach(
items: enumerate(local!len) + 1,
expression: local!data[fv!index]
)
),
local!all: a!forEach(
items: enumerate(local!len) + 1,
expression: local!data[fv!index]
),
a!forEach(
items: local!merge,
expression: index(
local!data,
wherecontains(fv!item, local!all),
{}
) & "---------------" & count(wherecontains(fv!item, local!all))
)
)

}

  Discussion posts and replies are publicly visible

Parents
  • Here's a somewhat different implementation - slightly longer but more focused on abstracting the individual steps on easier-to-understand local variable definitions, and without a need to iterate over an array of all 26 letters.

    a!localVariables(
      
      local!word: "banana",
      
      local!letters: a!forEach(
        code(lower(local!word)),
        char(fv!item)
      ),
      
      local!uniqueLetters: union(local!letters, local!letters),
      
      local!counts: a!forEach(
        local!uniqueLetters,
        a!map(
          letter: fv!item,
          count: length(wherecontains(fv!item, local!letters))
        )
      ),
      
      a!forEach(
        local!counts,    
        concat(repeat(fv!item.count, fv!item.letter)) & " ----> " & fv!item.count
      )
    )

Reply
  • Here's a somewhat different implementation - slightly longer but more focused on abstracting the individual steps on easier-to-understand local variable definitions, and without a need to iterate over an array of all 26 letters.

    a!localVariables(
      
      local!word: "banana",
      
      local!letters: a!forEach(
        code(lower(local!word)),
        char(fv!item)
      ),
      
      local!uniqueLetters: union(local!letters, local!letters),
      
      local!counts: a!forEach(
        local!uniqueLetters,
        a!map(
          letter: fv!item,
          count: length(wherecontains(fv!item, local!letters))
        )
      ),
      
      a!forEach(
        local!counts,    
        concat(repeat(fv!item.count, fv!item.letter)) & " ----> " & fv!item.count
      )
    )

Children