Skip to main content
swarnalashmir
January 23, 2024
Question

How to find duplicate word in string?

  • January 23, 2024
  • 4 replies
  • 0 views

Write a code to find the duplicate words in a string.

input string = "Big black bug bit a big black dog on his big black nose"

output: 

Duplicate words in a given string :
big
black

4 replies

harshitb6843
January 23, 2024

a!localVariables(
  local!string: "Big black bug bit a big black dog on his big black nose",
  local!array: split(local!string," "),
  local!uniqueItems: union(local!array,local!array),
  a!forEach(
    items: local!uniqueItems,
    expression: if(
      count(wherecontains(fv!item,local!array))>1,
      fv!item,
      {}
    )
  )
)

harshm4411
January 23, 2024

Handling Unnecessary spaces:

a!localVariables(
local!string: "Big black bug bit a big black dog on his big black nose nose ",
local!data: split(local!string, " "),
/* Handling null in case of unnecessary spaces */
local!list: reject(fn!isnull, local!data),
local!result: a!forEach(
items: local!list,
expression: if(
length(wherecontains(fv!item, local!list)) > 1,
fv!item,
{}
)
),
union(local!result, local!result)
)

harshitb6843
January 23, 2024

 [mention:7f7c3545bcbe47119ac428c885b27f95:e9ed411860ed4f2ba0265705b8793d05]  Glad to see you here. 

Try using this from next time for better readability.