Skip to main content
April 25, 2022
Question

find duplicate characters count in a string

  • April 25, 2022
  • 10 replies
  • 0 views

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))
)
)

}

10 replies

April 25, 2022

Hi,

Another way to achieve this logic

a!localVariables(
  local!word: "Banana",
  a!forEach(
    items: {
      "A","B","C","D","E","F","G","H", "I","J","K","L",
      "M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"
    },
    expression: a!localVariables(
      local!count: if(
        regexmatch(fv!item, upper(local!word), 1) > 0,
        count(regexallmatches(fv!item,local!word)),
        {}
      ),
      if(
        a!isNullOrEmpty(local!count),
        {},
        concat(repeat(local!count, fv!item), "-------", local!count)
      )
    )
  )
)

April 25, 2022

nice.

mikes0011
Brainy
April 25, 2022

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
  )
)

April 25, 2022

this is simple way and easy ..thank you @Mike Schmitt

mikes0011
Brainy
April 25, 2022

Thanks!

October 9, 2024

a!localVariables(
local!name: "ankit dahiya",
local!letters: a!forEach(code(local!name), char(fv!item)),
local!uniqueLetters: union(local!letters, local!letters),
touniformstring(
a!forEach(
items: remove(
local!uniqueLetters,
wherecontains(" ", local!uniqueLetters)
),
expression: {
fv!item & "-" & count(wherecontains(fv!item, local!letters))
}
)
)
)

October 12, 2024

a!localVariables(
  local!code: code("Banana"),
  local!distinctCode: union(local!code, local!code),
  a!forEach(
    local!distinctCode,
    a!localVariables(
      local!charCount: length(wherecontains(fv!item, local!code)),
      concat(
        repeat(local!charCount, char(fv!item)),
        "-------",
        local!charCount
      )
    )
  )
)

stefanhelzle0001
October 12, 2024

a!localVariables(
  local!word: "Banana",
  local!chars: char(code(local!word)),
  a!forEach(
    items: union(local!chars, local!chars),
    expression: concat(
      cleanwith(local!word, fv!item),
      "-------",
      len(cleanwith(local!word, fv!item))
    )
  )
)

October 14, 2024

a!localVariables(
local!string : "appian training",
local!chars : char(code(stripwith(local!string," "))),
local!uniquechars : union(local!chars,local!chars),
local!count : a!forEach(
items: local!uniquechars,
expression: a!map(
item : fv!item,
count : length(wherecontains(fv!item,local!chars))
)
),
local!output : a!forEach(
items: local!count,
expression: fv!item.item&"-------------"&fv!item.count,
),
local!output
)